Issue
I have calculated 9 matrix elements named sij, with i and j being variables (i,j = [1, 2, 3]). Here, i denotes rows and j columns. Suppose I want a 3x3 matrix that consists of the matrix elements s11, s12, ... s32, s33 (nine elements in total).
s11 = 1
s12 = 2
s13 = 3
(...)
s33 = 9
How can I use for loops to construct a matrix out of these elements? Like this:
matrix = [[s11, s12, s13], [s21, s22, s23], [s31, s32, s33]]
So that I get a matrix that looks like this.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Solution
I would consider renaming the sij
to s[i][j]
. Then using them in loops would be trivial.
s[1][1] = 1
s[1][2] = 2
s[1][3] = 3
(...)
s[3][3] = 9
Then:
instead of:
matrix = [[s11, s12, s13], [s21, s22, s23], [s31, s32, s33]]
You can have the following two nested loops to construct the matrix.
for i in (1,4):
for j in (1,4):
BTW, having a 0 based numbering would be more Pythonic.
Answered By - boardrider
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.