http://www.mathworks.com/matlabcentral/newsreader/view_thread/323991
MATLAB Central Newsreader  Sorting an array of points based on x values, but keeping original corresponding y values
Feed for thread: Sorting an array of points based on x values, but keeping original corresponding y values
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

Tue, 23 Oct 2012 21:10:06 +0000
Sorting an array of points based on x values, but keeping original corresponding y values
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323991#890166
Ryan
Hey all,<br>
<br>
To start, I am a very basic Matlab user. My problem is that I have 4 x values, all with f(x) values from an inline function and I need to sort the array with respect to only the x values, but I still need the f(x) values to "follow" wherever their original x values have shifted.<br>
<br>
Here is the array<br>
a = [2 4 1 5<br>
1 6 2 3]<br>
<br>
Where Row 1 is the x's and Row 2 is the f(x)'s.<br>
<br>
Somehow, I need matlab to arrange 'a' so that it looks like this:<br>
a = [1 2 4 5<br>
2 1 6 5]<br>
<br>
I have tried using sortrows(a) but it isnt aligning the values to their originals.<br>
<br>
Hopefully someone can help. Im sure it's an elementary fix and I just cant think of it.

Tue, 23 Oct 2012 21:58:06 +0000
Re: Sorting an array of points based on x values, but keeping original corresponding y values
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323991#890173
Roger Stafford
"Ryan" wrote in message <k6713e$j65$1@newscl01ah.mathworks.com>...<br>
> Hey all,<br>
> <br>
> To start, I am a very basic Matlab user. My problem is that I have 4 x values, all with f(x) values from an inline function and I need to sort the array with respect to only the x values, but I still need the f(x) values to "follow" wherever their original x values have shifted.<br>
> <br>
> Here is the array<br>
> a = [2 4 1 5<br>
> 1 6 2 3]<br>
> <br>
> Where Row 1 is the x's and Row 2 is the f(x)'s.<br>
> <br>
> Somehow, I need matlab to arrange 'a' so that it looks like this:<br>
> a = [1 2 4 5<br>
> 2 1 6 5]<br>
> <br>
> I have tried using sortrows(a) but it isnt aligning the values to their originals.<br>
> <br>
> Hopefully someone can help. Im sure it's an elementary fix and I just cant think of it.<br>
       <br>
[~,p] = sort(a(1,:),2);<br>
a = a(:,p);<br>
<br>
Roger Stafford