2

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.

Mathemats
  • 1,185
  • 4
  • 22
  • 35
aakash singh
  • 43
  • 1
  • 6

2 Answers2

2

If you have a rows:Seq[Seq[Double]], where each entry in the outer ~eq is a row:

DenseMatrix(rows:_*)

If you know the matrix dimensions ahead of time, and you have DenseVectors for each row/column)

val dm = DenseMatrix.zeros[Double](rows, cols)
dm(::, c) := myColDenseVector
dm(r, ::) := myRowDenseVector.t

Alternatively, if you have a bunch of DenseVectors, you can say: DenseVector.horzcat(columns:_*)

dlwh
  • 2,257
  • 11
  • 23
0

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
csch
  • 4,496
  • 2
  • 19
  • 23
aakash singh
  • 43
  • 1
  • 6