how to stack a 2D matrix form in 3D form?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
DGM
le 25 Nov 2021
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!