如何轻松地对任意指数进行切片



在Python的numpy中,我可以这样做:

>>> import numpy as np
>>> m = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
>>> indices = [1,3]
>>> m[:,indices]
array([[ 2,  4],
       [ 6,  8],
       [10, 12]])

换句话说,我可以基于任意(不一定是连续的)索引列表进行切片。我怎样才能在微风中做类似的事情?我想找一些高效的,最好是优雅的。

与numpy大致相同:

scala> import breeze.linalg._
import breeze.linalg._
scala> val m = DenseMatrix((1,2,3,4),(5,6,7,8),(9,10,11,12))
m: breeze.linalg.DenseMatrix[Int] =
1  2   3   4
5  6   7   8
9  10  11  12
scala> val indices = IndexedSeq(1,3)
indices: IndexedSeq[Int] = Vector(1, 3)
scala> m(::, indices)
res0: breeze.linalg.SliceMatrix[Int,Int,Int] =
2   4
6   8
10  12

最新更新