Extract a DenseVector from a DenseMatrix based on different column indices

18 views
Skip to first unread message

nejc....@gmail.com

unread,
Mar 26, 2018, 1:48:52 PM3/26/18
to Scala Breeze
Hello. I'm looking for a more efficient way of getting a DenseVector from a DenseMatrix where column index is different for every row. My current solution (colIndices is a DenseVector):
val result = 0 until matrix.rows map { rowIndex => val colIndex = colIndices(rowIndex) matrix(rowIndex, colIndex) }

David Hall

unread,
Mar 26, 2018, 2:25:28 PM3/26/18
to scala-...@googlegroups.com
same basic answers as the other one for efficiency.

If you just want economical, you can probably just do: DenseVector.tabulate(matrix.rows) { rowIndex => ... }

Or you can use a slice vector, which isn't a dense vector, but can be made into one.

matrix(colIndices.toIndexedSeq.zipWithIndex.map(_.swap)).toDenseVector

On Mon, Mar 26, 2018 at 2:47 AM, <nejc....@gmail.com> wrote:
Hello. I'm looking for a more efficient way of getting a DenseVector from a DenseMatrix where column index is different for every row. My current solution (colIndices is a DenseVector):
val result = 0 until matrix.rows map { rowIndex => val colIndex = colIndices(rowIndex) matrix(rowIndex, colIndex) }

--
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+unsubscribe@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/25125c6b-6ad2-48a6-b50a-a6af25d1963f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nejc....@gmail.com

unread,
Apr 5, 2018, 8:23:50 AM4/5/18
to Scala Breeze
Thank you for both responses.


On Monday, March 26, 2018 at 8:25:28 PM UTC+2, David Hall wrote:
same basic answers as the other one for efficiency.

If you just want economical, you can probably just do: DenseVector.tabulate(matrix.rows) { rowIndex => ... }

Or you can use a slice vector, which isn't a dense vector, but can be made into one.

matrix(colIndices.toIndexedSeq.zipWithIndex.map(_.swap)).toDenseVector
On Mon, Mar 26, 2018 at 2:47 AM, <nejc....@gmail.com> wrote:
Hello. I'm looking for a more efficient way of getting a DenseVector from a DenseMatrix where column index is different for every row. My current solution (colIndices is a DenseVector):
val result = 0 until matrix.rows map { rowIndex => val colIndex = colIndices(rowIndex) matrix(rowIndex, colIndex) }

--
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.
Reply all
Reply to author
Forward
0 new messages