Search code examples
scalamatrixlinear-algebrascala-breeze

Matrix Row manipulation in Breeze using Scala?


I've a DenseMatrix

1  2  3  0   0   0   0    0    0    
0  0  0  11  22  33  0    0    0    
0  0  0  0   0   0   111  222  333 

I want to remove the first row and then a last row with all 0s

0  0  0  11  22  33  0    0    0    
0  0  0  0   0   0   111  222  333 
0  0  0  0   0   0   0    0    0   

How do I achieve this in Breeze ?


Solution

  • First, gather the rows you still want:

    val subset = matrix(::, 2 to 3)
    

    then add the zeroes:

    val newMatrix = DenseMatrix.horzcat(subset, DenseMatrix.zeros[Double](1,9))
    

    I might have mixed up rows and columns in the last line.