I can not think of a for doing this multiplication, because the number of columns of the first array has to be equal to the number of rows of the second array ...
I can not think of a for doing this multiplication, because the number of columns of the first array has to be equal to the number of rows of the second array ...
Before making any code you need to know how the product works between arrays.
Considering two matrices A
and B
the matrix product is done by multiplying each value of a line of A
by each value of a column of B
and adding them all partials. This process is thus applied to each line of A
and column B
.
See an illustration of this process:
ConsideringarraysoftheA
illustrationwithdimension4x2
andB
2x3
,theresultingarray,whichI'llcallR
,willhavethe4x3
size.Hereyouseethattheresultingarraymusthavethenumberofrowsofthefirstarrayandthenumberofcolumnsofthesecondarray.
InthisarrayR
thefirsthouseR11
wouldthenbedefinedby:
R11=A11*B11+A12*B21;
ThesecondhouseR12
,whichishighlightedinthefigurewouldbecalculatedas:
R12=A11*B12+A12*B22;
Andthesameprocessappliestotheend.
Nowthatweknowhowtheproductworksbetweenarrays,wecanmoveontoimplementation.Youhaveindicatedafor
forsolvingtheproblemandinfactyouwillneed3forthis.Thefirst2for
cyclesthrougheachcelloftheresultingarrayand3
cyclesthroughAandBtodothecalculation.
intA[2][2]={{1,2},{3,4}};intB[2][2]={{2,0},{1,2}};intR[2][2]={0};inti,j,k;for(i=0;i<2;i++){for(j=0;j<2;j++){for(k=0;k<2;k++){R[i][j]+=A[i][k]*B[k][j];}}}
See the Ideone example
Note: The product between vector and array is the same thing as the product between array and array, since a vector is a particular case of a matrix with only one row or column.
Recommended reading: