get a diagonal line in matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, please help me get a diagonal line in matlab. Example, I have a matrix: a = [1 2 3; 4 5 6; 7 8 9] I want to get vector b =[3 5 7] which is a diagonal line of matrix a. This is my code but it doesn't work
a=[1 2 3; 4 5 6; 7 8 9];
b = zeros(1,3);
for i=1:3
for i=1:3
b = a(i,j);
end
end
0 commentaires
Réponse acceptée
Jan
le 21 Août 2017
Modifié(e) : Jan
le 21 Août 2017
Or:
a = [1 2 3; 4 5 6; 7 8 9];
s = size(a);
index = (numel(a) - s(1) + 1):(1 - s(1)):s(2);
b = a(index)
[EDITED, works with non square matrices now]
3 commentaires
Jan
le 22 Août 2017
If you really want a loop:
b = zeros(1,3);
for i = 1:3
b(i) = a(i, 4 - i);
end
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Operating on Diagonal Matrices 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!