I am in need of computing column sums in java using apache's RealMatrix. This would work like this:
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.MatrixUtils;
double[][] values = {{1.0, 2.0}, {3.0, 4.0}, {5.0, 6.0}};
RealMatrix matrix = MatrixUtils.createRealMatrix(values);
MatrixUtils.createRealMatrix(new double[][]{{1, 1, 1}}).multiply(matrix)
>> Array2DRowRealMatrix{{9.0,12.0}}
However, I would like to make it general when it comes to declaring ones in
MatrixUtils.createRealMatrix(new double[][]{{1, 1, 1}})
Is there a way to pre-declare the number of ones I want inside a curly bracket?
Say I want numberOfOnes = 10
, then:
MatrixUtils.createRealMatrix(new double[][]{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}})
I am struggling to find a way to make this general. Any help?
You can use Arrays.fill
:
double[][] m = new double[rows][cols];
for (int i = 0; i < rows; i++)
Arrays.fill(m[i], 1.0);