scalaapache-sparkbreezeapache-spark-mlscala-breeze

scala-breeze/spark replace a row of a densematrix with another densevector


I have a breeze.linalg.DenseMatrix:

breeze.linalg.DenseMatrix[Int] =  
1  5  9  
2  6  10  
3  7  11  
4  8  12  

and a breeze.linalg.DenseVector:

breeze.linalg.DenseVector[Int] = DenseVector(13, 14, 15)  

Slicing allows me to get a particular row of a DenseMatrix but not replace/reassign it. How can I replace one of the rows(for eg. 2nd row) of the matrix with the vector to get something as shown below?

1  5  9  
13 14 15  
3  7  11  
3  8  12  

Also, is there a way to achieve such matrix manipulations using any of Spark's matrix types? If yes, that would be much more desirable.


Solution

  • Slicing allows me to get a particular row of a DenseMatrix but not replace/reassign

    It does, you just need a correct shape - row slice is transposed

    val m = DenseMatrix((1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12))
    
    // breeze.linalg.Transpose[breeze.linalg.DenseVector[Int]] = Transpose(DenseVector(10, 11, 12))
    

    so you need matching transposed vector

    val v = DenseVector(10, 11, 12)
    m(1, ::) := v.t
    m
    
    // breeze.linalg.DenseMatrix[Int] =
    // 1   5   9
    // 10  11  12
    // 3   7   11
    // 4   8   12
    

    Also, is there a way to achieve such matrix manipulations using any of Spark's matrix types? If yes, that would be much more desirable.

    Spark ml.linalg and mllib.linalg are not full featured linear algebra tools and exist mostly to support other ml / mllib functions, so slicing, and modifications are not supported. You'll have to access underlying RDD and transform data manually with joins.