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

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

// 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
...
+5
source share
3 answers

Here is a simple example of the desired 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, it is:

enter image description here

+12
source

Why can't you use the Matrix arrayTimes method? A vector is just a 1 x n-matrix (I think), so you can't initialize a second matrix with just one dimension and use arrayTimes?

Matrix a = new Matrix( [[1,2,3],[1,2,3],[1,2,3]] );
Matrix b = new Matrix( [[1,2,3]] ); // this is a vector
Matrix c = a.arrayTimes(b.transpose); // transpose so that the inner dimensions agree

, , doc.

+1

:

double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
Matrix A = new Matrix(vals);

http://math.nist.gov/javanumerics/jama/doc/Jama/Matrix.html

0

All Articles