Search code examples
colt

How to combine two sparsematrix using java colt lib?


I'm using java colt library for sparse matrix. Inputs are the two sparse matrix with the same row number, e.g.,

SparseDoubleMatrix1D v1; //[1 2]
SparseDoubleMatrix1D v2; // [3, 4]

After some calculation of v1, and v2. I want to append the result of v2 to v1 or they can also be assigned to another new matrix, e.g.,

v3 = [v1; v2] ; // [1 2 3 4]

Any one know how to achieve this with colt? Is there an existing method for it?


Solution

  • Since no one answers, I find some solution by using DoubleFactory2D.sparse.appendColumns method in colt lib. details as below:

      SparseDoubleMatrix2D v1 = new SparseDoubleMatrix2D(new double[][]{{0, 1, 2}, {1, 1, 3}, {4, 5, 6}});
      SparseDoubleMatrix2D v2 = new SparseDoubleMatrix2D(new double[][]{{0, 1}, {1, 1}});
      SparseDoubleMatrix2D matrix = (SparseDoubleMatrix2D) DoubleFactory2D.sparse.appendColumns(v1, v2);