2

I have the following problem, I have a matrix generated by another program and written to a txt file, the file looks like this: ( I only included part of the matrix, total is 36 lines)

 1     1   7.225655E+03
 1     2   0.000000E+00
 1     3   0.000000E+00
 1     4   0.000000E+00
 1     5   2.384466E+04
 1     6   0.000000E+00
 2     1   0.000000E+00
 2     2   7.225655E+03         .
          .
          .
 5     5   4.175514E+06
 5     6   0.000000E+00
 6     1   0.000000E+00
 6     2  -2.829306E+03
 6     3   0.000000E+00
 6     4   0.000000E+00
 6     5   0.000000E+00
 6     6   3.916341E+06

I loaded this matrix already in a numpy array but now I would like to write a simple function which puts the values in a 6x6 matrix according to the indices on the first two columns.

1 Answer 1

2

If a is the tree-column matrix you've read from the file, the following should do it:

>>> m = np.zeros((6,6))
>>> for row, col, val in a:
...    m[row - 1, col - 1] = val
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.