繁体   English   中英

将2D矩阵与向量相乘以跨越三维-MATLAB

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

当我尝试将mxn矩阵与p-dimensional向量相乘时,我遇到了一些困难。

试图避免循环,这是我想要实现的目标

enter code here
M = [1 2 3;                   p = [1;2;3]
     4 5 6;
     7 8 9]

我想获得一个3x3x3矩阵,其中三维中的切片只是M的项乘以p的相应项。

非常感谢帮助

您可以使用bsxfunpermutevectorized (无环)的方式,像这样-

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

你最终会-

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

使用matrix-multiplication -

out = permute(reshape(reshape(M.',[],1)*p(:).',[size(M) numel(p)]),[2 1 3])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM