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:
Matrix trouble

Subject: Matrix trouble

From: Niall Heavey

Date: 10 Mar, 2009 21:54:01

Message: 1 of 3

Hi all,

Just wondering if there is any way of using the sort function (or if there is any other way) to do the following.

I am making a matrix.... lets say x = [4 2 6 -1; 6 2 5 1]
x =
4 2 6 -1
6 2 5 1

I want to try and sort out the top row in ascending order that will keep its corresponding y value under it.
For example, the sorted matrix, we can call it y, should be the following;
y =
-1 2 4 6
1 2 6 5

I hope that makes sense.

Anyone any idea how I might go about doing this?

Regards.

Subject: Matrix trouble

From: swgillan

Date: 11 Mar, 2009 02:42:15

Message: 2 of 3

On Mar 10, 2:54=A0pm, "Niall Heavey" <niallhea...@eircom.net> wrote:
> Hi all,
>
> Just wondering if there is any way of using the sort function (or if ther=
e is any other way) to do the following.
>
> I am making a matrix.... lets say x =3D [4 2 6 -1; 6 2 5 1]
> x =3D
> 4 2 6 -1
> 6 2 5 1
>
> I want to try and sort out the top row in ascending order that will keep =
its corresponding y value under it.
> For example, the sorted matrix, we can call it y, should be the following=
;
> y =3D
> -1 2 4 6
> 1 2 6 5
>
> I hope that makes sense.
>
> Anyone any idea how I might go about doing this?
>
> Regards.

I am not sure if this applies to every case, but I just through this
up in matlab

x =3D [4 2 6 -1; 6 2 5 1]

x =3D

     4 2 6 -1
     6 2 5 1

>> sortrows(x')' %transpose x, and then sortrows on it (column 1 ascending =
is default) then transpose it back.

ans =3D

    -1 2 4 6
     1 2 6 5

Subject: Matrix trouble

From: Roger Stafford

Date: 11 Mar, 2009 04:26:01

Message: 3 of 3

"Niall Heavey" <niallheavey@eircom.net> wrote in message <gp6nhp$2gd$1@fred.mathworks.com>...
> .....
> I want to try and sort out the top row in ascending order that will keep its corresponding y value under it.
> .....

 [p,p] = sort(x(1,:));
 y = x(:,p);

Roger Stafford

Tags for 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