I need this for matrix multiplication, I'm putting any 2 dimensional matrix into 1 dimension by column major order, i.e. the matrix

1 2
3 4

is stored as [1 3 2 4].

To try and figure it out I've worked out the pattern for 2x2 matrices:

data_[1] = data_[1]*b.data_[1] + data_[3]*b.data_[2]
data_[2] = data_[2]*b.data_[1] + data_[4]*b.data_[2]
data_[3] = data_[1]*b.data_[3] + data_[3]*b.data_[4]
data_[4] = data_[2]*b.data_[3] + data_[3]*b.data_[4]

and for 3x3 matrices:

data_[1] = data_[1]*b.data_[1] + data_[4]*b.data_[2] + data_[7]*b.data_[3]
data_[2] = data_[2]*b.data_[1] + data_[5]*b.data_[2] + data_[8]*b.data_[3]
data_[3] = data_[3]*b.data_[1] + data_[6]*b.data_[2] + data_[9]*b.data_[3]
data_[4] = data_[1]*b.data_[4] + data_[4]*b.data_[5] + data_[7]*b.data_[6]
data_[5] = data_[2]*b.data_[4] + data_[5]*b.data_[5] + data_[8]*b.data_[6]
data_[6] = data_[3]*b.data_[4] + data_[6]*b.data_[5] + data_[9]*b.data_[6]
data_[7] = data_[1]*b.data_[7] + data_[4]*b.data_[8] + data_[7]*b.data_[9]
data_[8] = data_[2]*b.data_[7] + data_[5]*b.data_[8] + data_[8]*b.data_[9]
data_[9] = data_[3]*b.data_[7] + data_[6]*b.data_[8] + data_[9]*b.data_[9]

but cant figure out how to put this into a general pattern for any size matrix, except for the obvious data_ = .....

can anyone see a way to do this?

Thanks in advance!

Edited 6 Years Ago by sexyzebra19: n/a

In general, matrices do not need to be square in order to compute their product... rather in computing A*B, the number of columns in matrix A must be equal to the number of rows in matrix B. Regardless...

We can think of each matrix as being composed of row and column vectors. Then, each entry [i,j] in the product matrix is the result of a dot product of a certain combination of these row/column vectors. We can denote the row vectors in matrix A as row[1] ... row[n] and the column vectors in matrix B as col[1] ... col[n].

Then, element [i,j] in the resultant product matrix would in fact be the result of the dot product <row, col[j]> , for 1<= i,j <= n

So the question is... how do you extract the column and row vectors using your particular representation of a matrix?

So the question is... how do you extract the column and row vectors using your particular representation of a matrix?

You wrote a bunch of information that didn't answer the question and then re-asked to OP's question.

OP: you can think of representing a 2D matrix in 1D as a counting operation. Basically, you count from zero to the # of elements in the column, then go back to zero and start again. This should remind you of a modulo operation, where as you increment an integer, it wraps back to 0 at the divisor. So:

6%8 == 6
7%8  == 7
8%8 == 0
9%8 == 1
10 %8 == 2

This is half of the method you would use to convert between 1D indices and 2D indices. The other half can be defined by using integer division. Integer division will basically tell you how many times you have counted up to some number ( like the # of elements in a column ).

6/8 == 0
14/8 == 1
22/8 == 2
30/8 == 3

Now, if you can combine an integer modulo opeation with an integer division operaton, you can extract 2D indices from 1D indices. I don't want to answer your question completely ( because it might be homework ), but I think this should point you in the right direction.

That's brilliant Dusktreader, thank you very much. Genius :) I will have a think about what you've said and see what I can do.

That's brilliant Dusktreader, thank you very much. Genius :) I will have a think about what you've said and see what I can do.

Thanks, but this is the standard math for doing these sorts of index conversions

This question has already been answered. Start a new discussion instead.