# Extract a DenseVector from a DenseMatrix based on different column indices

18 views

### nejc....@gmail.com

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

Mar 26, 2018, 2:25:28 PM3/26/18
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, 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.

### nejc....@gmail.com

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, 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.