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

3 次查看(过去 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) ?

采纳的回答

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