0 glao 4 Years Ago 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++) A[i]=... I know how to multiply 2 arrays as 2d ,but representing them as 1d? c++

-1 OPDiscussion Starter glao 4 Years Ago 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++){ result[i*R+j]+=A[i+k*N]*X[i*R+j]; } } } This doesn't work right.I can't figure how to deal with A matrix I think.

0 David W 131 4 Years Ago Is what you are trying to do is to transform a vector? i.e. M1*v1 => v2 i.e. Matrix1 times vector1 = vector2 ?

0 OPDiscussion Starter glao 4 Years Ago No,I am trying to multiply 2 2d arrays but dealing with them as 1d. I also tried: result[i*R+j]+=A[i*R+k]*X[k*R+j]; but still the same.. Edited 4 Years Ago by glao

0 David W 131 4 Years Ago 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

0 OPDiscussion Starter glao 4 Years Ago for (int i=0;i<n;i++){ for (int j=0;j<m;j++){ a[i*m+j]=0; } }

0 David W 131 4 Years Ago 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

0 Slavi 94 4 Years Ago 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?

0 OPDiscussion Starter glao 4 Years Ago 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]

0 OPDiscussion Starter glao 4 Years Ago Ok ,it goes like: for (int i=0;i<rows;i++){ for (int j=0;j<cols;j++){ result[i*cols+j]=0; for (int k=0;k<rows;k++){ result[i*cols+j]+=A[i*rows+k]*B[k*cols+j]; } } }