[英]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
的相應項。
非常感謝幫助
您可以使用bsxfun
與permute
為vectorized
(無環)的方式,像這樣-
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.