Hello everyone!
I have the following problem. I have an Nx2^N matrix of zeros and ones and I want to rearrange the columns as follows. First, in ascending order according to the column sum. Second, I want all columns that give the same column sum to be ordered AS VECTORS from greatest to smallest according to the lexicographical order (according to which vector x is greater than or equal to vector y if there is some j<=N such that x(i)=y(i) for all i<j (if any) and x(j)>y(j)).
For example, if the original matrix is:
A=[0 0 0 0 1 1 1 1;
0 0 1 1 0 0 1 1;
0 1 0 1 0 1 0 1]
(for N=3) I want to obtain:
B=[0 1 0 0 1 1 0 1;
0 0 1 0 1 0 1 1;
0 0 0 1 0 1 1 1]
Thanks! Alejandro

 채택된 답변

Doug Hull
Doug Hull 2012년 3월 27일

0 개 추천

If the matrix is the size you describe, and there are presumably all unique vectors, then I think all the options will be represented uniquely.
If that is the case, instead of 'sorting' the vectors, can you just 'generate' them all? It seems like it would be relatively easy to make a function to do this.
Here is some starter code:
flipud(sortrows(unique(perms([0 0 0]),'rows')))
flipud(sortrows(unique(perms([0 0 1]),'rows')))
flipud(sortrows(unique(perms([0 1 1]),'rows')))
flipud(sortrows(unique(perms([1 1 1]),'rows')))

댓글 수: 1

Alejandro
Alejandro 2012년 3월 27일
Indeed, all options are represented uniquely. So yes, generating the vectors in the desired order is easier than sorting an existing matrix and your suggestion works perfectly.
Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2012년 3월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by