Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
need help with sorting an array

Subject: need help with sorting an array

From: Faisal Ahmed

Date: 26 Jul, 2010 11:56:06

Message: 1 of 6

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

Subject: need help with sorting an array

From: us

Date: 26 Jul, 2010 12:06:05

Message: 2 of 6

"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

Subject: need help with sorting an array

From: Wayne King

Date: 26 Jul, 2010 12:10:36

Message: 3 of 6

"Faisal Ahmed" <fap87@yahoo.com> wrote in message <i2jt4m$6aa$1@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

Subject: need help with sorting an array

From: Faisal Ahmed

Date: 26 Jul, 2010 16:31:21

Message: 4 of 6

Thank you very much Wayne. Problem Solved !

Subject: need help with sorting an array

From: us

Date: 26 Jul, 2010 16:44:05

Message: 5 of 6

"Faisal Ahmed" <fap87@yahoo.com> wrote in message <i2kd8p$9r0$1@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

Subject: need help with sorting an array

From: Faisal Ahmed

Date: 26 Jul, 2010 16:52:04

Message: 6 of 6

"us " <us@neurol.unizh.ch> wrote in message <i2ke0l$s76$1@fred.mathworks.com>...
> "Faisal Ahmed" <fap87@yahoo.com> wrote in message <i2kd8p$9r0$1@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



us, I checked the doc sortrows later, you are right, using negative sign makes it even easier to understand. Thank you!

Thanks to all for your help.

-Faisal

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us