3

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.

8u88y
  • 33
  • 1
  • 6

1 Answers1

3

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.

Alper t. Turker
  • 34,230
  • 9
  • 83
  • 115
  • 1
    Works for me! Important takeaways for noobs like me: 1. Use elementwise assignment operator instead of assignment (:= instead of =) 2. A row slice is a transposed vector by default! – 8u88y Jan 12 '18 at 00:44
  • Could you prominently highlight the fact that elementwise assignment operator needs to be used? It is such a big thing which people can miss. – user238607 Jul 21 '20 at 15:27
  • Thanks! Your Q&A should be included in the Scala/Breeze cookbook! – Chien Nguyen May 26 '23 at 07:40