Namrata N Shahade
Namrata N Shahade

Reputation: 41

dot product in Matrix of Array in Java

I want to multiply (1 * n) and (1* n)

 x = [x0,x1,x2]
 y = [
       y0,
       y1,
       y2
      ]

Want to multiply by row * column eg: x0*y0, x1*y1... How can i iterate row into column. so that multiplication is expected. getting confused. Please help.

Upvotes: 0

Views: 2867

Answers (2)

João Castilho
João Castilho

Reputation: 487

In that case is simple! I will write in pseudo-code

int[] z = new int[3];
for (int i =0; i< x.length;i++)
z[i] = x[i] * y[i];

But these are not matrices, if you want matrices you have to do this

int [ ] [ ] x= new int [ 2 ] [ 2 ] ;
int [ ] [ ] y= new int [ 2 ] [ 2 ] ;
int [ ] [ ] z= new int [ 2 ] [ 2 ] ;
//Initialize the 2 matrices with values
for(int i=0;i<x[0].length;i++){
    for(int j=0;j<y.length;j++){
       z[i][j] = x[i][j] * y[j][i];
    }

}

Upvotes: 2

BUY
BUY

Reputation: 736

for(int i = 0; i < row.length; i++) {
    multiplicationOfRowsAndColumns[i] = row[i] * column[i]; 
}

This would do waht you want to do.

Upvotes: 0

Related Questions