Breeze - Boolean Matrix

117 views
Skip to first unread message

Wei

unread,
Dec 25, 2015, 8:33:23 PM12/25/15
to Scala Breeze
Happy Holidays, everyone.

I have been using matlab for many years and is happy to find a good library like Breeze providing similar functionality in Scala/Java. Just one quick question about indexing:

Is it possible to index dense matrix with boolean vector or matrix (this is commonly used in matlab)? Also, is boolean matrix boxed in Breeze?

Thanks.

David Hall

unread,
Dec 25, 2015, 8:39:16 PM12/25/15
to scala-...@googlegroups.com
Yes, you can index matrices and vectors with boolean tensors of the same type. The result is a "slicevector", which is probably not the best choice, but it is what it is.

Boolean matrices are currently boxed. 

scala> import breeze.linalg._
import breeze.linalg._

scala> val dm = DenseMatrix.rand(5, 5)
dm: breeze.linalg.DenseMatrix[Double] =
0.410491337822108    0.2312409497063863  0.10312792066860399  ... (5 total)
0.7890078434527261   0.9253947875291468  0.162569349496404    ...
0.24325903507125846  0.3187495767083455  0.21208038727461864  ...
0.2298254089467855   0.9683842795421511  0.099865086522966    ...
0.36268636924825404  0.9489842096973167  0.20128043935517237  ...

scala> dm(dm :> 0.5)
res2: breeze.linalg.SliceVector[(Int, Int),Double] = breeze.linalg.SliceVector@fea1c37b

scala> res2.toDenseVector
res3: breeze.linalg.DenseVector[Double] = DenseVector(0.799357260499427, 0.7890078434527261, 0.9253947875291468, 0.9357039251969395, 0.7484777558659128, 0.9683842795421511, 0.8659207280590215, 0.5086632429613545, 0.9489842096973167, 0.8484530630703433, 0.8750651441258643)

scala> dm(dm :> 0.5) := 1.0
res4: breeze.linalg.Vector[Double] = breeze.linalg.SliceVector@5b135003

scala> dm
res5: breeze.linalg.DenseMatrix[Double] =
0.410491337822108    0.2312409497063863  0.10312792066860399  ... (5 total)
1.0                  1.0                 0.162569349496404    ...
0.24325903507125846  0.3187495767083455  0.21208038727461864  ...
0.2298254089467855   1.0                 0.099865086522966    ...
0.36268636924825404  1.0                 0.20128043935517237  ...

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze...@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/4317092f-d50c-4f04-b0f6-45ac51d8feaa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wei

unread,
Dec 26, 2015, 11:28:59 AM12/26/15
to Scala Breeze
Thanks for the quick reply, David.

The Boolean indexing is awesome and I really like it. Since this kind of indexing could me used often (especially in x(x > 0) kind of scenario), could you please consider supporting Boolean as unboxed?

Thanks again for the excellent work in Breeze. I can definitely see more and more academic and industry use with the library. 
Reply all
Reply to author
Forward
0 new messages