简体   繁体   English

Scala Breeze扩展矩阵

[英]Scala Breeze expansion matrix

In Octave/Matlab I can expand an identity matrix as follows. 在Octave / Matlab中,我可以如下扩展一个单位矩阵。

>> I = eye(3)
I =
Diagonal Matrix
  1   0   0
  0   1   0
  0   0   1

>> A = [ 3 2 3 2 2 1 3 2 2 1 ]

>> E = I(:, A)
E =
   0   0   0   0   0   1   0   0   0   1
   0   1   0   1   1   0   0   1   1   0
   1   0   1   0   0   0   1   0   0   0

How can I achieve the same thing (ie obtain E from A, possibly using I) with Scala/Breeze ? 如何使用Scala /微风实现同一件事(即从A获得E,可能使用I)?

Got it. 得到它了。 Actually very similar to Octave. 其实很像八度。

scala> val I = DenseMatrix.eye[Int](3)
I: breeze.linalg.DenseMatrix[Int] =
1  0  0
0  1  0
0  0  1

scala> val A = DenseMatrix(2, 1, 2, 1, 1, 0, 2, 1, 1, 0) // zero based in breeze

scala> I(::, A.toArray.toSeq)
res26: breeze.linalg.SliceMatrix[Int,Int,Int] =
0  0  0  0  0  1  0  0  0  1
0  1  0  1  1  0  0  1  1  0
1  0  1  0  0  0  1  0  0  0

The caveats are: 注意事项是:

  • the matrices must contain Integers 矩阵必须包含整数
  • indices are 0 based (as opposed to 1 based in Octave/Matlab) 索引基于0(与Octave / Matlab中基于1的索引相反)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM