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:
sorting a 3D array by columns only?

Subject: sorting a 3D array by columns only?

From: mpalmer@lmi.net

Date: 15 Jan, 2009 19:29:40

Message: 1 of 6

I am trying to sort an array by columns only, not rows, and nothing works.

Here's an example of what I am looking for:

Original array:
px(:,:,1) = [100 255 255 0]
px(:,:,2) = [100 50 0 255];
px(:,:,3) = [255 255 255 200]

Sorted:
sx(:,:,1) = [ 0 100 255 255]
sx(:,:,2) = [255 100 0 50]
sx(:,:,3) = [200 255 255 255]

As you see, each column is sorted from top row to bottom, without affecting the individual ordering of the rows.

Subject: sorting a 3D array by columns only?

From: mpalmer@lmi.net

Date: 15 Jan, 2009 19:31:46

Message: 2 of 6

(I didn't quite say that right, hope it's clear! What i mean is for each column to be taken as a single entity in the sort, not to be broken up.)

Subject: sorting a 3D array by columns only?

From: Bruno Luong

Date: 15 Jan, 2009 20:29:02

Message: 3 of 6

"mpalmer@lmi.net" <mpalmer@lmi.net> wrote in message <26799119.1232047811301.JavaMail.jakarta@nitrogen.mathforum.org>...

>
> As you see, each column is sorted from top row to bottom ...

Sorry I can't see it.

Bruno

Subject: sorting a 3D array by columns only?

From: Matt

Date: 15 Jan, 2009 20:41:01

Message: 4 of 6

"mpalmer@lmi.net" <mpalmer@lmi.net> wrote in message <26799119.1232047811301.JavaMail.jakarta@nitrogen.mathforum.org>...
> I am trying to sort an array by columns only, not rows, and nothing works.
>
> Here's an example of what I am looking for:
>
> Original array:
> px(:,:,1) = [100 255 255 0]
> px(:,:,2) = [100 50 0 255];
> px(:,:,3) = [255 255 255 200]
>
> Sorted:
> sx(:,:,1) = [ 0 100 255 255]
> sx(:,:,2) = [255 100 0 50]
> sx(:,:,3) = [200 255 255 255]
>
> As you see, each column is sorted from top row to bottom, without affecting the individual ordering of the rows.

sz=size(px);
ppx=permute(px,[3 2 1]).' ;
ssx=sortrows(ppx).' ;
sx=ipermute(ssx,[3 2 1]);

Subject: sorting a 3D array by columns only?

From: Roger Stafford

Date: 15 Jan, 2009 20:57:02

Message: 5 of 6

"mpalmer@lmi.net" <mpalmer@lmi.net> wrote in message <26799119.1232047811301.JavaMail.jakarta@nitrogen.mathforum.org>...
> I am trying to sort an array by columns only, not rows, and nothing works.
>
> Here's an example of what I am looking for:
>
> Original array:
> px(:,:,1) = [100 255 255 0]
> px(:,:,2) = [100 50 0 255];
> px(:,:,3) = [255 255 255 200]
>
> Sorted:
> sx(:,:,1) = [ 0 100 255 255]
> sx(:,:,2) = [255 100 0 50]
> sx(:,:,3) = [200 255 255 255]
>
> As you see, each column is sorted from top row to bottom, without affecting the individual ordering of the rows.

  My documentation doesn't state what 'sortrows' (which I assume you are using here) will do with multi-dimensional arrays. From your results I would say that, since the first dimension is a singleton, it is ignored. It is sorting the 2nd dimension columns in accordance with the "dictionary" order in the 3rd dimension. For example (0 255 200) in px along the 3rd dimension is the least in this ordering and therefore comes first in sx.

  I guess Mathworks can be forgiven for this since you apparently are working outside their rules. I wonder what it would do if the first dimension were not a singleton.

Roger Stafford

Subject: sorting a 3D array by columns only?

From: mpalmer@lmi.net

Date: 15 Jan, 2009 22:41:40

Message: 6 of 6

Thanks for that neat trick, Matt!

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