how to stack a 2D matrix form in 3D form?
조회 수: 4 (최근 30일)
이전 댓글 표시
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) ?
댓글 수: 0
채택된 답변
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)
% 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)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!