Hi,
I have a matrix of m x m^2 and I want to find the diagonals of the sub matrices m x m, then rearrange the matrix A such that the sub matrix with the largest diagonal is first and the sub matrix with the lowest diagonal is last
E.g. For the matrix A = [A1 A2 A3]
A =
2 4 6 8 9 1 4 8 1
4 1 2 3 6 1 2 3 4
5 2 3 4 1 7 2 3 12
Sub matrix A1 has a diagnoal of 6 Sub matrix A2 has a diagonal of 21 Sub matrix A3 has a diagonal of 19
so the final output would be A = [A2 A3 A1]
A =
8 9 1 4 8 1 2 4 6
3 6 1 2 3 4 4 1 2
4 1 7 2 3 12 5 2 3
Thanks in advance

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 6월 29일
편집: Andrei Bobrov 2014년 6월 29일

2 개 추천

[~,ii] = sort(sum(reshape(A(repmat(eye(3),1,3)>0),3,[])),'descend');
A1 = reshape(A,3,[],3);
out = reshape(A1(:,:,ii),3,[]);
or
Ac = mat2cell(A,3,[3 3 3]);
[~,ii] = sort(cellfun(@trace,Ac),'descend');
out = cell2mat(Ac(ii));

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

질문:

2014년 6월 29일

편집:

2014년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by