How do I vector product by matrix and between matrices?

1

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 ...

    
asked by anonymous 16.10.2017 / 00:45

1 answer

2

Dot Matrix

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:

ConsideringarraysoftheAillustrationwithdimension4x2andB2x3,theresultingarray,whichI'llcallR,willhavethe4x3size.Hereyouseethattheresultingarraymusthavethenumberofrowsofthefirstarrayandthenumberofcolumnsofthesecondarray.

InthisarrayRthefirsthouseR11wouldthenbedefinedby:

R11=A11*B11+A12*B21;

ThesecondhouseR12,whichishighlightedinthefigurewouldbecalculatedas:

R12=A11*B12+A12*B22;

Andthesameprocessappliestotheend.

CalculationinC

Nowthatweknowhowtheproductworksbetweenarrays,wecanmoveontoimplementation.Youhaveindicatedaforforsolvingtheproblemandinfactyouwillneed3forthis.Thefirst2forcyclesthrougheachcelloftheresultingarrayand3cyclesthroughAandBtodothecalculation.

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:

16.10.2017 / 02:59