How to read elements of a matrix diagonally in MATLAB?

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

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 9 月 18 日
編集済み: Andrei Bobrov 2013 年 9 月 18 日

1 投票

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';

4 件のコメント

Febin Benjamin
Febin Benjamin 2013 年 9 月 18 日
編集済み: Febin Benjamin 2013 年 9 月 18 日
Thanx Andrei! It worked! And what if I had to read the same matrix as shown below?
7
5 1
23 0 2
3 17
9
Jan
Jan 2013 年 9 月 18 日
@Andrei: I'm impressed. After reading the question I do not have the faintest idea what "reading a matrix in a specific fashion" could mean and what the shown list of value is.
Febin Benjamin
Febin Benjamin 2013 年 9 月 18 日
@Jan: I tried hard to frame the question in a comprehensive way but that's what I managed to come up with. :) Fortunately, Andrei got it! hehe :)
Andrei Bobrov
Andrei Bobrov 2013 年 9 月 18 日
B = [2,5,7; 3,0,1; 9,17,23];
A = fliplr(B); % and etc

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索

製品

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by