Multiplication of 1D and 2D vector to a 3D vector
10 次查看(过去 30 天)
显示 更早的评论
I have A = [1,2] and B = [1,1;1,1], what function I can use to get result of answer C which is 3D?
C(:,:,1) = [1,1
1,1]
C(:,:,2) = [1,1
1,1]
Is there anyway to realize it without loop?
0 个评论
回答(4 个)
madhan ravi
2020-10-21
编辑:madhan ravi
2020-10-21
C = reshape(A, 1, 1, []) .* B % use bsxfun() if you’re version is prior to 2016b
C = bsxfun(@times, reshape(A, 1, 1, []), B)
0 个评论
madhan ravi
2020-10-21
Sigh. Your question says one thing and your example does another thing. Only God know what you want.
C = repmat(B, 1, 1, max(A))
0 个评论
madhan ravi
2020-10-21
[m, n] = size(B);
C = zeros(m, n, numel(A));
for k = 1:numel(A)
C(:, :, A(k)) = B;
end
C
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!