python - Construct a matrix using a for loop -
i have calculated 9 matrix elements named sij, , j being variables (i,j = [1, 2, 3]). here, denotes rows , j columns. suppose want 3x3 matrix consists of matrix elements s11, s12, ... s32, s33 (nine elements in total).
s11 = 1 s12 = 2 s13 = 3 (...) s33 = 9 how can use loops construct matrix out of these elements? this:
matrix = [[s11, s12, s13], [s21, s22, s23], [s31, s32, s33]] so matrix looks this.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
i consider renaming sij s[i][j]. using them in loops 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 following 2 nested loops construct matrix.
for in (1,4): j in (1,4): btw, having 0 based numbering more pythonic.
Comments
Post a Comment