Matrix and List

I’m trying to express a matrix by using Python, for example a 3X3 matrix with numbers:

1 2 3

4 5 6

7 8 9

can be also writen as ([1,2,3,4,5,6,7,8,9], 3), where number 3

shows the number of columns in the matrix. I AM NOT TRYING TO PRINT THE PATTERN OF THE MATRIX. Another example,

1 3 5

2 4 6

becomes ([1,3,5,2,4,6], 3).

This submatrix can be defined as a (l,r,t,b) tuple, where l and r are left and

right columns, and t and b are top and bottom rows (all

inclusive).

Similarly,
submatrix(([1,2,3,4,5,6,7,8,9,10,11,12], 4), (1,2,0,1)) returns:

([2,3,6,7], 2)

because, ([1,2,3,4,5,6,7,8,9,10,11,12], 4) represents:

1 2 3 4

5 6 7 8

9 10 11 12

and (1,2,0,1) represents the submatrix between columns 1 and 2 (both inclusive), and rows 0 and 1 (both inclusive). So, the result becomes

2 3

6 7

which is also equal to ([2,3,6,7],2).

I am also trying to write it with the definition function. Like so: def submatrix(matrix, indices)
How can this be achieved?

I am also trying to write it with the definition function.

Wouldn’t a Class be better?
And while I’d think nested arrays would be better, even without them, it’s just a matter of clever slicing the array.

submatrix(([1,2,3,4,5,6,7,8,9,10,11,12], 4), (1,2,0,1))

So you want all the elements inbetween 1 from left, to 2 from right, 0 from top and 1 from bottom.
Per definition you got an array which is to be interpreted as 4 rows, meaning indices 0-3 are the first row, 4-7 the second, 8-11 the third.
So now you just need to combine those facts together.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.