Multiplication of 1D and 2D vector to a 3D vector

조회 수: 11 (최근 30일)
Liu Lilingjun
Liu Lilingjun 2020년 10월 21일
답변: madhan ravi 2020년 10월 21일
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?

답변 (4개)

madhan ravi
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)

madhan ravi
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))

madhan ravi
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

madhan ravi
madhan ravi 2020년 10월 21일
C = repelem(B, 1, 1, max(A))

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by