row-broadcasting and transposed vectors?

风格不统一 提交于 2019-12-11 12:05:21

问题


I'm trying to add a row vector to each row of a matrix:

val M = DenseMatrix((1.0,2.0,3.0),
                    (4.0,5.0,6.0))

val row = DenseVector(3.0,4.0,5.0).t

val result = M(*,::) + row
// error: could not find implicit value for parameter 
// op: OpAdd.Impl2[
//   BroadcastedRows[DenseMatrix[Double],DenseVector[Double]],
//   Transpose[DenseVector[Double]],
//   That
// ]

I can use column vectors instead, but it seems a little convoluted:

val result = (M.t(::,*) + row.t).t

// result: breeze.linalg.DenseMatrix[Double] =
// 4.0  6.0  8.0
// 7.0  9.0  11.0

Thanks.

来源:https://stackoverflow.com/questions/32745724/row-broadcasting-and-transposed-vectors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!