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?
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?
Jump to PostWhere is your 2D array in the code you provided?
Jump to PostIs what you are trying to do is to transform a vector?
i.e. M1*v1 => v2
i.e. Matrix1 times vector1 = vector2 ?
Jump to PostCan 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
Where is your 2D array in the code you provided?
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.
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:
result[i*R+j]+=A[i*R+k]*X[k*R+j];
but still the same..
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++){
a[i*m+j]=0;
}
}
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++){
result[i*cols+j]=0;
for (int k=0;k<rows;k++){
result[i*cols+j]+=A[i*rows+k]*B[k*cols+j];
}
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.