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:
Matlab problem with the "sort" function

Subject: Matlab problem with the "sort" function

From: fiatlux

Date: 27 Sep, 2010 19:44:41

Message: 1 of 8

Hello,
First of all, sorry for my English.
I have a set of N points in a Nx3 matrix (1 line = 1 point, each points has 2 coordinates and one random value). The first column is the x coordinate, the 2nd column is the y coordinate and the 3rd column is the number of nearest neighbors in a 8-pixels radius, so let's just say that each line (that is: each point) has a corresponding (x,y,K) vector, K being more or less a random number.
My problem is the following: I want to sort those points from the largest K to the smallest K. But if I use the sort function along the 3rd column, the K values will be sorted but the (x,y) values will not have been moved with their corresponding K, so they won't correspond anymore!
So do you have any idea how to solve this? Because I'm currently trying to do it with several for loops but it's just ugly and slow...

Thanks in advance!

Subject: Matlab problem with the "sort" function

From: TideMan

Date: 27 Sep, 2010 20:02:19

Message: 2 of 8

On Sep 28, 8:44 am, fiatlux <dex...@hotmail.fr> wrote:
> Hello,
> First of all, sorry for my English.
> I have a set of N points in a Nx3 matrix (1 line = 1 point, each points has 2 coordinates and one random value). The first column is the x coordinate, the 2nd column is the y coordinate and the 3rd column is the number of nearest neighbors in a 8-pixels radius, so let's just say that each line (that is: each point) has a corresponding (x,y,K) vector, K being more or less a random number.
> My problem is the following: I want to sort those points from the largest K to the smallest K. But if I use the sort function along the 3rd column, the K values will be sorted but the (x,y) values will not have been moved with their corresponding K, so they won't correspond anymore!
> So do you have any idea how to solve this? Because I'm currently trying to do it with several for loops but it's just ugly and slow...
>
> Thanks in advance!

help sortrows

If A is your Nx3 matrix
B=sortrows(A,3);
will sort A based on column 3.

Subject: Matlab problem with the "sort" function

From: Matt Fig

Date: 27 Sep, 2010 20:06:04

Message: 3 of 8

See the help for SORTROWS. Pay particular attention to the two-input option.

Subject: Matlab problem with the "sort" function

From: fiatlux

Date: 27 Sep, 2010 20:17:40

Message: 4 of 8

Thanks a lot to both of you! I didn't know that function

Subject: Matlab problem with the "sort" function

From: dpb

Date: 27 Sep, 2010 20:23:45

Message: 5 of 8

fiatlux wrote:
> Thanks a lot to both of you! I didn't know that function

Are you aware of "lookfor" ?

 > lookfor sort
CPLXPAIR Sort numbers into complex conjugate pairs.
SORT Sort in ascending order.
SORTROWS Sort rows in ascending order.
SORT Sort for cell arrays of strings.
 >>

Very useful if don't know actual function but have idea of keyword or
topic...

--

Subject: Matlab problem with the "sort" function

From: Jan Simon

Date: 27 Sep, 2010 20:49:19

Message: 6 of 8

Dear fiatlux,

Or:
  A = rand(100, 3);
  [dummy, index] = sort(A(:, 3));
  SortedA = A(index, :)

Kind regards, Jan

Subject: Matlab problem with the "sort" function

From: fiatlux

Date: 27 Sep, 2010 20:58:57

Message: 7 of 8

wow I didn't know that function neither! That's awesome, thanks a lot :)

Subject: Matlab problem with the "sort" function

From: fiatlux

Date: 27 Sep, 2010 21:36:54

Message: 8 of 8

Thank you very much! Nice approach as well.

Tags for this Thread

No tags are associated with 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