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:
rearrange matrix or vector elements by finding the index

Subject: rearrange matrix or vector elements by finding the index

From: Jorge

Date: 1 Nov, 2010 16:09:04

Message: 1 of 8

Hi,
I need to find the Index of a matrix that rearranges it like in the following example:
I have this Vector: A = [5 6 4 4 5 4 5 7];
and B = sort(A) is equal to: B = [4 4 4 5 5 5 6 7];
I need to find the Index X so that A = B(X)
Is there a function for that?

Thanx Jorge

Subject: rearrange matrix or vector elements by finding the index

From: Sean

Date: 1 Nov, 2010 16:29:05

Message: 2 of 8

"Jorge " <jorge.ebinger@db.com> wrote in message <iamon0$lrr$1@fred.mathworks.com>...
> Hi,
> I need to find the Index of a matrix that rearranges it like in the following example:
> I have this Vector: A = [5 6 4 4 5 4 5 7];
> and B = sort(A) is equal to: B = [4 4 4 5 5 5 6 7];
> I need to find the Index X so that A = B(X)
> Is there a function for that?
>
> Thanx Jorge

Look at the second output argument to sort!
doc sort

 [A, ix] = sort([3 4 5 7 6])
%{
ix = [1 2 3 5 4]
%}

Subject: rearrange matrix or vector elements by finding the index

From: Miroslav Balda

Date: 1 Nov, 2010 16:33:05

Message: 3 of 8

"Jorge " <jorge.ebinger@db.com> wrote in message <iamon0$lrr$1@fred.mathworks.com>...
> Hi,
> I need to find the Index of a matrix that rearranges it like in the following example:
> I have this Vector: A = [5 6 4 4 5 4 5 7];
> and B = sort(A) is equal to: B = [4 4 4 5 5 5 6 7];
> I need to find the Index X so that A = B(X)
> Is there a function for that?
>
> Thanx Jorge

help sort

Mira

Subject: rearrange matrix or vector elements by finding the index

From: Matt J

Date: 1 Nov, 2010 16:34:04

Message: 4 of 8

[B,Index]=sort(A);

Subject: rearrange matrix or vector elements by finding the index

From: Matt J

Date: 1 Nov, 2010 16:46:03

Message: 5 of 8

"Jorge " <jorge.ebinger@db.com> wrote in message <iamon0$lrr$1@fred.mathworks.com>...
> Hi,
> I need to find the Index of a matrix that rearranges it like in the following example:
> I have this Vector: A = [5 6 4 4 5 4 5 7];
> and B = sort(A) is equal to: B = [4 4 4 5 5 5 6 7];
> I need to find the Index X so that A = B(X)
> Is there a function for that?
======

Sorry, we all read your post to fast. You want the indexing X that inverts the sort operation.

One way is


[B,idx] = sort(A);
[~,X]=sort(idx);

Subject: rearrange matrix or vector elements by finding the index

From: Matt J

Date: 1 Nov, 2010 16:49:03

Message: 6 of 8

This might be faster than applying sort() twice:

P=speye(length(A));
P=P(idx,:);
[~,X]=max(P,[],1)


Not sure, though.

Subject: rearrange matrix or vector elements by finding the index

From: Steven_Lord

Date: 1 Nov, 2010 17:25:40

Message: 7 of 8



"Jorge " <jorge.ebinger@db.com> wrote in message
news:iamon0$lrr$1@fred.mathworks.com...
> Hi, I need to find the Index of a matrix that rearranges it like in the
> following example:
> I have this Vector: A = [5 6 4 4 5 4 5 7];
> and B = sort(A) is equal to: B = [4 4 4 5 5 5 6 7];
> I need to find the Index X so that A = B(X)
> Is there a function for that?

A = [5 6 4 4 5 4 5 7]
[B, idx] = sort(A)
clear idx2 % if it already exists
idx2(idx) = 1:numel(idx)
B - A(idx) % should be all zero
A - B(idx2) % should also be all zero

--
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: rearrange matrix or vector elements by finding the index

From: Bruno Luong

Date: 1 Nov, 2010 18:33:06

Message: 8 of 8

"Steven_Lord" <slord@mathworks.com> wrote in message <iamt6k$kje$1@fred.mathworks.com>...

>
> A = [5 6 4 4 5 4 5 7]
> [B, idx] = sort(A)
> clear idx2 % if it already exists
> idx2(idx) = 1:numel(idx)

Steve's method is the best of finding the reverse permutation.

Bruno

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