From: Faisal Ahmed on
Suppose I have the following array

1.0000 2.0000 0.4231
1.0000 3.0000 0.8050
1.0000 4.0000 0.7116
2.0000 4.0000 0.6682
2.0000 5.0000 0.4460
2.0000 7.0000 0.5818
3.0000 4.0000 0.0977
3.0000 6.0000 0.5989
3.0000 7.0000 0.3237
4.0000 7.0000 0.2882
5.0000 6.0000 0.1456
5.0000 7.0000 0.3168
6.0000 7.0000 0.2767

I want to sort rows according to the elements of column 3 in a descending order.
So that the sorted array is like:
1.0000 3.0000 0.8050
1.0000 4.0000 0.7116
2.0000 4.0000 0.6682
3.0000 6.0000 0.5989
.......................................
.......................................

Note that, if I use 'sortrows' then the array is sorted in a ascending order automatically which I could not change. If I use 'sort', each column is sorted individually which is not what I want. Any help would be highly appreciated.

-Faisal
From: us on
"Faisal Ahmed"
> Note that, if I use 'sortrows' then the array is sorted in a ascending order automatically which I could not change. If I use 'sort', each column is sorted individually which is not what I want. Any help would be highly appreciated.
>
> -Faisal

well...
carefully peruse

doc sortrows;
% in particular, look at the second input args...
% eg, what happens if you change the sign of one of them(?)...

us
From: Wayne King on
"Faisal Ahmed" <fap87(a)yahoo.com> wrote in message <i2jt4m$6aa$1(a)fred.mathworks.com>...
> Suppose I have the following array
>
> 1.0000 2.0000 0.4231
> 1.0000 3.0000 0.8050
> 1.0000 4.0000 0.7116
> 2.0000 4.0000 0.6682
> 2.0000 5.0000 0.4460
> 2.0000 7.0000 0.5818
> 3.0000 4.0000 0.0977
> 3.0000 6.0000 0.5989
> 3.0000 7.0000 0.3237
> 4.0000 7.0000 0.2882
> 5.0000 6.0000 0.1456
> 5.0000 7.0000 0.3168
> 6.0000 7.0000 0.2767
>
> I want to sort rows according to the elements of column 3 in a descending order.
> So that the sorted array is like:
> 1.0000 3.0000 0.8050
> 1.0000 4.0000 0.7116
> 2.0000 4.0000 0.6682
> 3.0000 6.0000 0.5989
> .......................................
> .......................................
>
> Note that, if I use 'sortrows' then the array is sorted in a ascending order automatically which I could not change. If I use 'sort', each column is sorted individually which is not what I want. Any help would be highly appreciated.
>
> -Faisal

Hi Faisal,

X = [ 1.0000 2.0000 0.4231
1.0000 3.0000 0.8050
1.0000 4.0000 0.7116
2.0000 4.0000 0.6682
2.0000 5.0000 0.4460
2.0000 7.0000 0.5818
3.0000 4.0000 0.0977
3.0000 6.0000 0.5989
3.0000 7.0000 0.3237
4.0000 7.0000 0.2882
5.0000 6.0000 0.1456
5.0000 7.0000 0.3168
6.0000 7.0000 0.2767];
[Y,I] = sort(X(:,3),'descend');
X1 = X(I,:);
% Now look at X1

Wayne
From: Faisal Ahmed on
Thank you very much Wayne. Problem Solved !
From: us on
"Faisal Ahmed" <fap87(a)yahoo.com> wrote in message <i2kd8p$9r0$1(a)fred.mathworks.com>...
> Thank you very much Wayne. Problem Solved !

well... yes, but considering the ease of

r=sortrows(X,-3); % <- one simple line...
isequal(X1,r)
% ans = 1

us