If I want to multiple 2d arrays as 1d how should I do it?

I mean ,I have a 2d array and I map it as 1d:

for(int i=0;i<rows*cols;i++)

I know how to multiply 2 arrays as 2d ,but representing them as 1d?

It doesn't matter.
I just try to multiply 2 arrays.

For example let's say we have A matrix (2x2) and X matrix (2x1)and we want to compute result matrix(2x1):

int N=2;
int R=1;
for (int i=0;i<N;i++){
   for (int j=0;j<R;j++){
     for (int k=0;k<R;k++){

This doesn't work right.I can't figure how to deal with A matrix I think.

Is what you are trying to do is to transform a vector?

i.e. M1*v1 => v2
i.e. Matrix1 times vector1 = vector2 ?

No,I am trying to multiply 2 2d arrays but dealing with them as 1d.

I also tried:


but still the same..

Edited 2 Years Ago by glao

Can you show your map of a 'n by m' Matrix to a vector of length n*m ?

And then do regular matrix multipliction ( with appropriate size checks) ...

Then ... M1*M2 => M3

should map to ...

vm1*vm2 => vm3

for (int i=0;i<n;i++){
    for (int j=0;j<m;j++){


That is not a map of M -> v ...

v is all set to zero there,

( but it shows the general idea,
if zero is replaced by M[i][j] )

Now show your code for ...

M1*M2 => M3

with row & column checking

and the appropriate size for M3

I showed my code,above..row is N and column is N..

are you trying to multiply each element of the matrix with a corresponding element in the vector? as in element 1 in the matrix * element 1 in the vector?

I am trying to multiply 2 2d arrays (just for this example the number of rows is 2(N) and number of columns is 1(R).

The A matrix is (NN) and X is (NR) , so the result is (N*R).

Normally,I would do :  result[i][j]=A[i][k]*X[k][j]

Ok ,it goes like:

 for (int i=0;i<rows;i++){
   for (int j=0;j<cols;j++){
     for (int k=0;k<rows;k++){


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