how to create a 3D diagonal matrix from a 2D matrix ?
Afficher commentaires plus anciens
Hi, I have a 2D matrix A=rand(3,5) and i want to create a 3D matrix B, where
B(1,:,:) = diag(A(1,:)) ; B(2,:,:) = diag(A(2,:)) ; B(3,:,:) = diag(A(3,:))
so the final size of B is expected to be 3x5x5
I am looking for a command to calculate B without passing through loops or for .
thanks
4 commentaires
Matt Fig
le 23 Août 2012
I am confused about the point of this....
B = rand(3,5,5);
C = rand(3,5,5);
D = rand(3,5);
try
x1 = (B-C).\D % Error
catch
disp('first one made an error!')
x2 = (B-C)\D % Error
end
What are you trying to do again??
Matt Fig
le 23 Août 2012
But that is not what you wrote above! You clearly have B and C as 3D and the equation you show produces an error. So which is it? 3D or 2D? How are we to help if you say 3D one time and 2D the next??
yasser
le 23 Août 2012
Réponse acceptée
Plus de réponses (1)
Sean de Wolski
le 23 Août 2012
Modifié(e) : Sean de Wolski
le 23 Août 2012
So each slice of B should contain a column vector (like you have above)? Why not just use a FOR-loop? or bsxfun? For example:
x = bsxfun(@minus,C,A).\D
Actually, bsxfun won't fill in zeros here so a for-loop is your best bet.
2 commentaires
yasser
le 23 Août 2012
Sean de Wolski
le 23 Août 2012
And how is a for-loop not perfectly applicable to that?
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!