簡體   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