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 an array of points based on x values, but keeping original corresponding y values

Subject: Sorting an array of points based on x values, but keeping original corresponding y values

From: Ryan

Date: 23 Oct, 2012 21:10:06

Message: 1 of 2

Hey all,

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.

Here is the array
a = [2 4 1 5
     1 6 2 3]

Where Row 1 is the x's and Row 2 is the f(x)'s.

Somehow, I need matlab to arrange 'a' so that it looks like this:
a = [1 2 4 5
     2 1 6 5]

I have tried using sortrows(a) but it isnt aligning the values to their originals.

Hopefully someone can help. Im sure it's an elementary fix and I just cant think of it.

Subject: Sorting an array of points based on x values, but keeping original corresponding y values

From: Roger Stafford

Date: 23 Oct, 2012 21:58:06

Message: 2 of 2

"Ryan" wrote in message <k6713e$j65$1@newscl01ah.mathworks.com>...
> Hey all,
>
> 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.
>
> Here is the array
> a = [2 4 1 5
> 1 6 2 3]
>
> Where Row 1 is the x's and Row 2 is the f(x)'s.
>
> Somehow, I need matlab to arrange 'a' so that it looks like this:
> a = [1 2 4 5
> 2 1 6 5]
>
> I have tried using sortrows(a) but it isnt aligning the values to their originals.
>
> Hopefully someone can help. Im sure it's an elementary fix and I just cant think of it.
- - - - - - - -
 [~,p] = sort(a(1,:),2);
 a = a(:,p);

Roger Stafford

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