Sorting Vectors descending order

I appreciate if anyone could help me for writing below sorting for different vectors: Assumed :
A1= (a1,a2)
A2= (a3,a4)
A3=(a5,a6)
.
.
An= (ai, aj)
How can I sort A1, A2, … ,An. by just first member comparison descending order (1) or second comparison (2), or both member comparison (3): solution : (for example)
1) a3>a1>a5 ---> output: A2, A1, A5...
or
2) a6>a4>a2 ----> output: A3,A2,A1
3) a1>a3 >a5 and a2>a4 >a6 --->output: A1, A2,A3

 采纳的回答

Star Strider
Star Strider 2014-8-26

1 个投票

The sortrows function is probably what you want. See specifically Sort Rows of a Matrix

6 个评论

Thank you Star Strider, The out put is very important for me. I want to reach to A1, A2, A3. after sorting the matrix I can not figure out A1,A2,A3 orders. Can you help me please.
My pleasure!
Can you be a bit more specific about what A1 ... An are, and what a1, a2, ... ai, aj are? Are they individual array elements, vectors, or something else?
Thank you. For example:
a=[1,5,9];
b=[2,6,10];
c=[3,7,11];
d=[4,8,12];
e=[a;d;b;c]
B=sortrows(e)
then the answer is
B =
1 5 9
2 6 10
3 7 11
4 8 12
so I want the above matrix (solution)to show me the vectors name instead of value. for above example B=[a;b;c;d] I mean I made e with vector names but I received a vector with values how can I reach to a sorted matrix with vector names?
Superb! It took some experimenting, but this seems to work:
a=[1,5,9];
b=[2,6,10];
c=[3,7,11];
d=[4,8,12];
ec = {'a' 'd' 'b' 'c'}; % Matrix as cell array by vector name
e = cell2mat(cellfun(@eval, ec, 'uni',0)') % Create matrix from ‘ec’
[B,I] = sortrows(e) % Sort rows
vn = ec(I) % Sorted vector names
I created a cell array of the vector names in ‘ec’ in the order you want them in matrix ‘e’. I then created matrix ‘e’ by evaluating the cell array in the order you specified, using cellfun and then creating a numerical matrix from it with cell2mat. I then sorted ‘e’ with sortrows, and used the index vector ‘I’ to create the sorted array names in ‘vn’.
My code produces:
e =
1 5 9
4 8 12
2 6 10
3 7 11
B =
1 5 9
2 6 10
3 7 11
4 8 12
I =
1
3
4
2
vn =
'a' 'b' 'c' 'd'
Is that what you want?
Awesome! Perfect Answer. Thank you very much. Yes it was what I wanted and I appreciate for your time.
My pleasure!
That was a really fun problem for me!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by