By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

Need to help in create the matrix AxB in python

P: 2
I need to create matrix AxB
The requirement is as sample below
This is the 5x10 matrix that I need

Expand|Select|Wrap|Line Numbers
  1. [1.0, 0.0, 0.0, 0.0, 0.0],
  2. [-1.0, 0.0, 0.0, 0.0, 0.0],
  3. [0.0, 1.0, 0.0, 0.0, 0.0],
  4. [0.0, -1.0, 0.0, 0.0, 0.0],
  5. [0.0, 0.0, 1.0, 0.0, 0.0],
  6. [0.0, 0.0, -1.0, 0.0, 0.0],
  7. [0.0, 0.0, 0.0, 1.0, 0.0],
  8. [0.0, 0.0, 0.0, -1.0, 0.0],
  9. [0.0, 0.0, 0.0, 0.0, 1.0],
  10. [0.0, 0.0, 0.0, 0.0, -1.0]
Would anyone help me in this case?

I have tried with
Expand|Select|Wrap|Line Numbers
  1. test_matrix = [[0.0 for i in range(5)] for i in range(10)]
  2.  
  3. for d1 in range(5):
  4.     test_matrix[d1][d1] = 1.0
But it create as
Expand|Select|Wrap|Line Numbers
  1. [1.0, 0.0, 0.0, 0.0, 0.0],
  2. [0.0, 1.0, 0.0, 0.0, 0.0],
  3. [0.0, 0.0, 1.0, 0.0, 0.0],
  4. [0.0, 0.0, 0.0, 1.0, 0.0],
  5. [0.0, 0.0, 0.0, 0.0, 1.0],
  6. [0.0, 0.0, 0.0, 0.0, 0.0],
  7. [0.0, 0.0, 0.0, 0.0, 0.0],
  8. [0.0, 0.0, 0.0, 0.0, 0.0],
  9. [0.0, 0.0, 0.0, 0.0, 0.0],
  10. [0.0, 0.0, 0.0, 0.0, 0.0]
May 13 '16 #1
Share this Question
Share on Google+
1 Reply


P: 2
Here is the solution

Expand|Select|Wrap|Line Numbers
  1. matrix_test = [[0.0 for i in range(5)] for i in range(10)]
  2. for x in range(5):
  3.     matrix_test [2 * x][x] = 1.0
  4.     matrix_test [2 * x + 1][x] = -1.0
May 13 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.