Search code examples
scalascala-breeze

How to add rows and columns dynamically to a Breeze DenseMatrix in Scala


I am hard-coding a matrix with the values below:

val m = breeze.linalg.DenseMatrix((1, 4), (2, 5))
val v = breeze.linalg.DenseMatrix((7, 8), (3, 6))

I want to insert these values on the rows and columns by reading from lists.


Solution

  • This is what I found out:

    val x =  new DenseMatrix(2, 3, Array(11, 12, 13, 21, 22, 23))
    

    which gives the matrix with 2 rows and 3 columns by slicing off the numbers 2 and 3 specified from the array.

    The result is:

    11  13  22
    12  21  23