How to read elements of a matrix diagonally in MATLAB?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Suppose A is a 3X3 matrix
A=[2,5,7; 3,0,1; 9,17,23]
I want to read the elements in this fashion.
2
5 3
9 0 7
1 17
23
0 commentaires
Réponse acceptée
Andrei Bobrov
le 18 Sep 2013
Modifié(e) : Andrei Bobrov
le 18 Sep 2013
A=[2,5,7; 3,0,1; 9,17,23]
k = spdiags(rot90(A));
s = size(k);
nns = nan(s);
ptr = tril(triu(ones(s)),s(1)-1);
nns(ptr>0) = k(ptr>0);
nns(:,1:2:end) = nns(end:-1:1,1:2:end);
out = sort(ptr,'descend');
out(out>0) = nns(~isnan(nns));
out = out';
or
A=[2,5,7; 3,0,1; 9,17,23];
ds = spdiags(rot90(A));
l = spdiags(ones(size(A)));
ds(:,1:2:end) = flipud(ds(:,1:2:end));
l(:,1:2:end) = flipud(l(:,1:2:end));
l2 = sort(l,'descend');
l2(l2>0) = ds(l>0);
out = l2';
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!