How can I multiply a matrix by a vector using JAMA?

user843337 picture user843337 · Jul 13, 2011 · Viewed 11k times · Source

I'm trying to create a vector from an array of doubles. I then want to multiply this vector by a matrix. Does anyone know how I can achieve this? Below is a really simple example that I would like to get working.

// Create the matrix (using JAMA)
Matrix a = new Matrix( [[1,2,3],[1,2,3],[1,2,3]] );

// Create a vector out of an array
...

// Multiply the vector by the matrix
...

Answer

Grzegorz Szpetkowski picture Grzegorz Szpetkowski · Jul 13, 2011

Here is simple example of wanted operation:

double[][] array = {{1.,2.,3},{1.,2.,3.},{1.,2.,3.}}; 
Matrix a = new Matrix(array);   
Matrix b = new Matrix(new double[]{1., 1., 1.}, 1);     
Matrix c = b.times(a);  
System.out.println(Arrays.deepToString(c.getArray()));

Result:

[[3.0, 6.0, 9.0]]

In other words that is:

enter image description here