http://www.mathworks.com/matlabcentral/newsreader/view_thread/242488
MATLAB Central Newsreader  sorting a 3D array by columns only?
Feed for thread: sorting a 3D array by columns only?
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 15 Jan 2009 19:29:40 +0000
sorting a 3D array by columns only?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242488#621896
mpalmer@lmi.net
I am trying to sort an array by columns only, not rows, and nothing works.<br>
<br>
Here's an example of what I am looking for:<br>
<br>
Original array:<br>
px(:,:,1) = [100 255 255 0]<br>
px(:,:,2) = [100 50 0 255];<br>
px(:,:,3) = [255 255 255 200]<br>
<br>
Sorted:<br>
sx(:,:,1) = [ 0 100 255 255]<br>
sx(:,:,2) = [255 100 0 50]<br>
sx(:,:,3) = [200 255 255 255]<br>
<br>
As you see, each column is sorted from top row to bottom, without affecting the individual ordering of the rows.

Thu, 15 Jan 2009 19:31:46 +0000
Re: sorting a 3D array by columns only?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242488#621897
mpalmer@lmi.net
(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.)

Thu, 15 Jan 2009 20:29:02 +0000
Re: sorting a 3D array by columns only?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242488#621902
Bruno Luong
"mpalmer@lmi.net" <mpalmer@lmi.net> wrote in message <26799119.1232047811301.JavaMail.jakarta@nitrogen.mathforum.org>...<br>
<br>
> <br>
> As you see, each column is sorted from top row to bottom ...<br>
<br>
Sorry I can't see it.<br>
<br>
Bruno

Thu, 15 Jan 2009 20:41:01 +0000
Re: sorting a 3D array by columns only?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242488#621903
Matt
"mpalmer@lmi.net" <mpalmer@lmi.net> wrote in message <26799119.1232047811301.JavaMail.jakarta@nitrogen.mathforum.org>...<br>
> I am trying to sort an array by columns only, not rows, and nothing works.<br>
> <br>
> Here's an example of what I am looking for:<br>
> <br>
> Original array:<br>
> px(:,:,1) = [100 255 255 0]<br>
> px(:,:,2) = [100 50 0 255];<br>
> px(:,:,3) = [255 255 255 200]<br>
> <br>
> Sorted:<br>
> sx(:,:,1) = [ 0 100 255 255]<br>
> sx(:,:,2) = [255 100 0 50]<br>
> sx(:,:,3) = [200 255 255 255]<br>
> <br>
> As you see, each column is sorted from top row to bottom, without affecting the individual ordering of the rows.<br>
<br>
sz=size(px);<br>
ppx=permute(px,[3 2 1]).' ;<br>
ssx=sortrows(ppx).' ;<br>
sx=ipermute(ssx,[3 2 1]);

Thu, 15 Jan 2009 20:57:02 +0000
Re: sorting a 3D array by columns only?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242488#621906
Roger Stafford
"mpalmer@lmi.net" <mpalmer@lmi.net> wrote in message <26799119.1232047811301.JavaMail.jakarta@nitrogen.mathforum.org>...<br>
> I am trying to sort an array by columns only, not rows, and nothing works.<br>
> <br>
> Here's an example of what I am looking for:<br>
> <br>
> Original array:<br>
> px(:,:,1) = [100 255 255 0]<br>
> px(:,:,2) = [100 50 0 255];<br>
> px(:,:,3) = [255 255 255 200]<br>
> <br>
> Sorted:<br>
> sx(:,:,1) = [ 0 100 255 255]<br>
> sx(:,:,2) = [255 100 0 50]<br>
> sx(:,:,3) = [200 255 255 255]<br>
> <br>
> As you see, each column is sorted from top row to bottom, without affecting the individual ordering of the rows.<br>
<br>
My documentation doesn't state what 'sortrows' (which I assume you are using here) will do with multidimensional 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.<br>
<br>
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.<br>
<br>
Roger Stafford

Thu, 15 Jan 2009 22:41:40 +0000
Re: sorting a 3D array by columns only?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242488#621927
mpalmer@lmi.net
Thanks for that neat trick, Matt!