Multiply 2D Matrix with vector to span third dimension - MATLAB

China☆狼群 提交于 2019-11-29 15:33:18

You can use bsxfun with permute for a vectorized (no-loop) approach like so -

out = bsxfun(@times,M,permute(p(:),[3 2 1]))

You would end up with -

out(:,:,1) =
     1     2     3
     4     5     6
     7     8     9
out(:,:,2) =
     2     4     6
     8    10    12
    14    16    18
out(:,:,3) =
     3     6     9
    12    15    18
    21    24    27

With matrix-multiplication -

out = permute(reshape(reshape(M.',[],1)*p(:).',[size(M) numel(p)]),[2 1 3])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!