0

I have got a three-dimensional matrix

sigma = zeros(3, 3, 1000)

and a single matrix

result = [0.5 0.5 0.5; 0.2 0.2 0.2; 0.1 0.1 0.1]

My question would be: What is the most effective way to add my matrix 'result' to every single matrix along the third dimension of 'sigma'?

Currently I am looping over all 1000 matrices and add them one by one. Is there a Matlab-optimized way?

Thanks!

4

2 回答 2

4

Use bsxfun:

bsxfun(@plus, sigma, result)

It automatically replicates the smaller matrix along its singleton dimension to match the other matrix.

于 2013-03-19T18:28:19.983 回答
2

sigma + repmat(结果,[1,1,1000])

一般来说,传递函数的调用(尽管比循环更有效)在底层的优化不如只涉及本机代码的调用。

于 2013-03-19T18:43:21.000 回答