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:
Sort a Matrix according to another Matrix's Descending order

Subject: Sort a Matrix according to another Matrix's Descending order

From: Nehal

Date: 20 Aug, 2010 16:04:05

Message: 1 of 5

I have a matrix:

A= [1 2 3
       4 5 6
       7 8 9]

I have another matrix:

B = [5 9 2]

first I want to sort matrix-B in descending order.

And then as the column moves in B, the columns of A will also move according to B's movement.

How can I do it..? Any suggestion..?

Subject: Sort a Matrix according to another Matrix's Descending order

From: Walter Roberson

Date: 20 Aug, 2010 16:07:44

Message: 2 of 5

On 20/08/10 11:04 AM, Nehal wrote:
> I have a matrix:
>
> A= [1 2 3
> 4 5 6
> 7 8 9]
>
> I have another matrix:
>
> B = [5 9 2]
>
> first I want to sort matrix-B in descending order.
>
> And then as the column moves in B, the columns of A will also move
> according to B's movement.
>
> How can I do it..? Any suggestion..?

Use the two-output version of sort() to get the index order from sorting
B, and then access A at that index order.

[vals, idx] = sort(B,2);
A(:,idx)

Subject: Sort a Matrix according to another Matrix's Descending order

From: Nehal

Date: 20 Aug, 2010 16:28:21

Message: 3 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <kLxbo.56367$F%7.49000@newsfe10.iad>...
> On 20/08/10 11:04 AM, Nehal wrote:
> > I have a matrix:
> >
> > A= [1 2 3
> > 4 5 6
> > 7 8 9]
> >
> > I have another matrix:
> >
> > B = [5 9 2]
> >
> > first I want to sort matrix-B in descending order.
> >
> > And then as the column moves in B, the columns of A will also move
> > according to B's movement.
> >
> > How can I do it..? Any suggestion..?
>
> Use the two-output version of sort() to get the index order from sorting
> B, and then access A at that index order.
>
> [vals, idx] = sort(B,2);
> A(:,idx)

[vals, idx] = sort(B,2);
what does the "2" do here..? and how will matlab understand whether I am asking for descending order or ascending order..?

Subject: Sort a Matrix according to another Matrix's Descending order

From: Steven_Lord

Date: 20 Aug, 2010 16:35:33

Message: 4 of 5



"Nehal " <arnab620@yahoo.com> wrote in message
news:i4maf5$l1u$1@fred.mathworks.com...
> Walter Roberson <roberson@hushmail.com> wrote in message
> <kLxbo.56367$F%7.49000@newsfe10.iad>...
>> On 20/08/10 11:04 AM, Nehal wrote:
>> > I have a matrix:
>> >
>> > A= [1 2 3
>> > 4 5 6
>> > 7 8 9]
>> >
>> > I have another matrix:
>> >
>> > B = [5 9 2]
>> >
>> > first I want to sort matrix-B in descending order.
>> >
>> > And then as the column moves in B, the columns of A will also move
>> > according to B's movement.
>> >
>> > How can I do it..? Any suggestion..?
>>
>> Use the two-output version of sort() to get the index order from sorting
>> B, and then access A at that index order.
>>
>> [vals, idx] = sort(B,2);
>> A(:,idx)
>
> [vals, idx] = sort(B,2);
> what does the "2" do here..?

Exactly what the documentation for SORT says it does. Type the following
command in MATLAB to learn what it does:

doc sort

> and how will matlab understand whether I am asking for descending order or
> ascending order..?

That, too, you can control using an input to SORT that will be described in
the page above.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Sort a Matrix according to another Matrix's Descending order

From: Nehal

Date: 20 Aug, 2010 18:12:04

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <i4mask$im8$1@fred.mathworks.com>...
>
>
> "Nehal " <arnab620@yahoo.com> wrote in message
> news:i4maf5$l1u$1@fred.mathworks.com...
> > Walter Roberson <roberson@hushmail.com> wrote in message
> > <kLxbo.56367$F%7.49000@newsfe10.iad>...
> >> On 20/08/10 11:04 AM, Nehal wrote:
> >> > I have a matrix:
> >> >
> >> > A= [1 2 3
> >> > 4 5 6
> >> > 7 8 9]
> >> >
> >> > I have another matrix:
> >> >
> >> > B = [5 9 2]
> >> >
> >> > first I want to sort matrix-B in descending order.
> >> >
> >> > And then as the column moves in B, the columns of A will also move
> >> > according to B's movement.
> >> >
> >> > How can I do it..? Any suggestion..?
> >>
> >> Use the two-output version of sort() to get the index order from sorting
> >> B, and then access A at that index order.
> >>
> >> [vals, idx] = sort(B,2);
> >> A(:,idx)
> >
> > [vals, idx] = sort(B,2);
> > what does the "2" do here..?
>
> Exactly what the documentation for SORT says it does. Type the following
> command in MATLAB to learn what it does:
>
> doc sort
>
> > and how will matlab understand whether I am asking for descending order or
> > ascending order..?
>
> That, too, you can control using an input to SORT that will be described in
> the page above.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thank You. I got it.

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