how to stack a 2D matrix form in 3D form?

조회 수: 4 (최근 30일)
chan
chan 2021년 11월 25일
답변: DGM 2021년 11월 25일
Could anyone suggest me some hint to solve this problem. i have a 3D matrix A=6*3*6 and length(U)=3 where U values are 3,4,5. I want the updated calculation of A in 3D form. how can we do that?
for j=1:length(U)
M=A(U(j),1,2)* A(:,:,U(j));//stack 1
//stack 2 value will be for the next iteration i.e U(2)=4
//stack 3
How can i get a 6*3*length(U) ?

채택된 답변

DGM
DGM 2021년 11월 25일
Something like this
A = randi(9,[6 3 6]);
U = 3:5;
% do it with a loop
M = zeros(size(A,1),size(A,2),numel(U));
for k = 1:length(U)
M(:,:,k) = A(U(k),1,2) * A(:,:,U(k));
end
Alternatively, this can be vectorized like so:
% if you're running R2016b or newer
M2 = permute(A(U,1,2),[3 2 1]).*A(:,:,U);
% show that this is the same as using the loop
immse(M,M2)
ans = 0
% if you're running R2016a or older
M3 = bsxfun(@times,permute(A(U,1,2),[3 2 1]),A(:,:,U));
% show that this is the same as using the loop
immse(M,M3)
ans = 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by