0

In Python's numpy, I can do this:

>>> 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]])

In other words, I can slice based on an arbitrary (not necessarily contiguous) list of indices. How can I do something similar in Breeze? I'm looking for something efficient and preferably elegant.

Mark
  • 1,788
  • 1
  • 22
  • 21

1 Answers1

4

More or less identically to 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
dlwh
  • 2,257
  • 11
  • 23
  • Thanks for the fast response! When I try the same thing you've shown here, I get "error: could not find implicit value for parameter canSlice: breeze.linalg.support.CanSlice2[breeze.linalg.DenseMatrix[Int],collection.immutable.::.type,IndexedSeq[Int],Result]. I'm using version 2.10-0.7. Is this feature newer than that? If so, any suggested workarounds (other than upgrading)? – Mark Sep 04 '14 at 15:50
  • I'm working on moving to a more current version, BTW, it just may take a while to do it. – Mark Sep 04 '14 at 16:16
  • Was just able to test it on 2.10-0.8.1. Same problem. – Mark Sep 04 '14 at 16:59
  • This bug/limitation was fixed only in 0.9. You can either use m(0 until m.rows, indices) or upgrade, or include this implicit in scope: https://github.com/scalanlp/breeze/commit/d05dbca62bf593fdba49b9c3f1a08b522cfbd6cb#diff-6de1c25cb44fd0c654b6c1b5cf199181R766 – dlwh Sep 04 '14 at 19:57
  • Worked perfectly, thanks! And thanks again for the prompt support! – Mark Sep 05 '14 at 15:01