我的目标是乘矩阵和向量VEC:乘以向量和矩阵
public static int[] vectorProduct(int[][] a, int[] x){
if(x.length != a[0].length)
throw new RuntimeException("Illegal matrix dimensions.");
int [] y = new int[a.length];
for(int i = 0; i<y.length;i++){
int sum=0;
for(int j = 0; j < a[0].length; j++){
sum+= a[i][j] * x[j];
}
}
return y;
}
我的主类看起来是这样的:
public static void main(String[] args){
int [][] d = { {1,2,3},{1,2,3},{1,2,3}};
int [] vec = { 1,1,1 };
System.out.println("Vector product of matrix A and vecor x: ");
int[] v = MatMath.vectorProduct(d, vec);
print(v);
}
我应该得到[3, 6,9],但我得到[0,0,0]作为我的输出。我究竟做错了什么?
你永远不会做的变量'sum'什么。你可能应该把它放在你的'y'数组中,因为你要返回它 – Gab