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 array with re-occuring values

Subject: sorting array with re-occuring values

From: Michael

Date: 19 Feb, 2009 00:12:07

Message: 1 of 13

Suppose:

>> I=[3 7 10 8 4 5 9 5 8 2]

Does anyone know a quick solution how I can use "I" as an index (same
number of elements as "I", first element is "1", and no re-occurring
values)?

That array I want should be look like this:

>>iWant
   = 2 6 10 7 3 4 9 5 8 1

Thanks a lot!

-Michael

Subject: sorting array with re-occuring values

From: Matt Fig

Date: 19 Feb, 2009 00:29:02

Message: 2 of 13

Michael <michael.j.krause@gmail.com> wrote in message <09fd2fcb-3a08-414a-872a-7578408ea0cf@g3g2000pre.googlegroups.com>...
> Suppose:
>
> >> I=[3 7 10 8 4 5 9 5 8 2]
>
> Does anyone know a quick solution how I can use "I" as an index (same
> number of elements as "I", first element is "1", and no re-occurring
> values)?
>
> That array I want should be look like this:
>
> >>iWant
> = 2 6 10 7 3 4 9 5 8 1
>
> Thanks a lot!
>
> -Michael



I don't see the connection between I and iWant. Especially when you say that the first element should be 1, then you show the first element as 2, and nothing looks sorted to me at all. What exactly is the mapping you are after?





H[@f]`VR!HJff\fLWL4ffmVmOL,V[fI[PStHOHTfWIHUOVVNJPS'VL\HHTU

Subject: sorting array with re-occuring values

From: dpb

Date: 19 Feb, 2009 00:38:32

Message: 3 of 13

Michael wrote:
> Suppose:
>
>>> I=[3 7 10 8 4 5 9 5 8 2]
>
> Does anyone know a quick solution how I can use "I" as an index (same
> number of elements as "I", first element is "1", and no re-occurring
> values)?
>
> That array I want should be look like this:
>
>>> iWant
> = 2 6 10 7 3 4 9 5 8 1

I have no clue how you specifically transformed from I-->iWant

If you can quantify that, there's undoubtedly a way...

--

Subject: sorting array with re-occuring values

From: Michael

Date: 19 Feb, 2009 00:41:55

Message: 4 of 13

On Feb 18, 4:29=A0pm, "Matt Fig" <spama...@yahoo.com> wrote:
> Michael <michael.j.kra...@gmail.com> wrote in message <09fd2fcb-3a08-414a=
-872a-7578408ea...@g3g2000pre.googlegroups.com>...
> > Suppose:
>
> > >> I=3D[3 =A0 =A0 7 =A0 =A010 =A0 =A0 8 =A0 =A0 4 =A0 =A0 5 =A0 =A0 9 =
=A0 =A0 5 =A0 =A0 8 =A0 =A0 2]
>
> > Does anyone know a quick solution how I can use "I" as an index (same
> > number of elements as "I", first element is "1", and no re-occurring
> > values)?
>
> > That array I want should be look like this:
>
> > >>iWant
> > =A0 =A0=3D 2 =A0 =A0 6 =A0 =A0 10 =A0 =A0 7 =A0 =A0 3 =A0 =A0 4 =A0 =A0=
 9 =A0 =A0 5 =A0 =A0 8 =A0 =A0 1
>
> > Thanks a lot!
>
> > -Michael
>
> I don't see the connection between I and iWant. =A0Especially when you sa=
y that the first element should be 1, then you show the first element as 2,=
 and nothing looks sorted to me at all. =A0What exactly is the mapping you =
are after?
>
> H[@f]`VR!HJff\fLWL4ffmVmOL,V[fI[PStHOHTfWIHUOVVNJPS'VL\HHTU- Hide quoted =
text -
>
> - Show quoted text -

Sorry for the confusion. I try to disambiguate. The smallest element
in "I" is "2". In "iWant" I set that to "1". The next element in "I"
is "3", I set that to "2" in "iWant", and so on. The first reoccuring
element in "I" is "5" (I(8)). I set that to "5" in "iWant" (the first
"5" was set to "4").

"iWant" is an index of "I". Hope things are more clear now.

Subject: sorting array with re-occuring values

From: Matt Fig

Date: 19 Feb, 2009 00:45:04

Message: 5 of 13

The only connection I see is this:

iWant = I - [1 1 0 1 1 1 0 0 0 1]

I still see nothing sorted, and I don't see how you want the second vector to be generated. Please try again to say what you want and be very specific about the mapping.





MYK/iQp^OZC_7XKYYiZiKcXOLiSRKWUpOYO^VVMSii$YWKK*RwK_iR^Y`iL

Subject: sorting array with re-occuring values

From: Michael

Date: 19 Feb, 2009 00:55:13

Message: 6 of 13

On Feb 18, 4:45=A0pm, "Matt Fig" <spama...@yahoo.com> wrote:
> The only connection I see is this:
>
> iWant =A0=3D I - [1 1 0 1 1 1 0 0 0 1]
>
> I still see nothing sorted, and I don't see how you want the second vecto=
r to be generated. =A0Please try again to say what you want and be very spe=
cific about the mapping.
>
> MYK/iQp^OZC_7XKYYiZiKcXOLiSRKWUpOYO^VVMSii$YWKK*RwK_iR^Y`iL

"I" has 10 elements.
>> sort(I)

ans =3D

     2 3 4 5 5 7 8 8 9 10

I want to assign an array to "I" that has the same number of elements
as "I", but starts at "1" (iWant(1)=3D1). "iWant" then increases
steadily to the last element in "I". When ever an element re-occurs
in "I", iWant just increases by 1. A simple example:

x=3D[3;2;2]

The transfer function should turn this into

xx
 =3D 3 2 1

Thanks for hanging on.

Subject: sorting array with re-occuring values

From: dpb

Date: 19 Feb, 2009 01:00:22

Message: 7 of 13

Michael wrote:
...
> I want to assign an array to "I" that has the same number of elements
> as "I", but starts at "1" (iWant(1)=1). "iWant" then increases
> steadily to the last element in "I". ...

By that description what you want is

iWant = [1:length(I)]

--

Subject: sorting array with re-occuring values

From: Roger Stafford

Date: 19 Feb, 2009 01:05:18

Message: 8 of 13

Michael <michael.j.krause@gmail.com> wrote in message <09fd2fcb-3a08-414a-872a-7578408ea0cf@g3g2000pre.googlegroups.com>...
> Suppose:
>
> >> I=[3 7 10 8 4 5 9 5 8 2]
>
> Does anyone know a quick solution how I can use "I" as an index (same
> number of elements as "I", first element is "1", and no re-occurring
> values)?
>
> That array I want should be look like this:
>
> >>iWant
> = 2 6 10 7 3 4 9 5 8 1
>
> Thanks a lot!
>
> -Michael

  It isn't clear from your description, but I suspect you want this:

 [t,p] = sort(I);
 q = 1:size(I,2);
 q(p) = q;

In other words you want the inverse of the permutation that is returned by sorting I. The above is an easy way of finding this inverse.

Roger Stafford

Subject: sorting array with re-occuring values

From: NZTideMan

Date: 19 Feb, 2009 01:09:04

Message: 9 of 13

On Feb 19, 1:12=A0pm, Michael <michael.j.kra...@gmail.com> wrote:
> Suppose:
>
> >> I=3D[3 =A0 =A0 7 =A0 =A010 =A0 =A0 8 =A0 =A0 4 =A0 =A0 5 =A0 =A0 9 =A0=
 =A0 5 =A0 =A0 8 =A0 =A0 2]
>
> Does anyone know a quick solution how I can use "I" as an index (same
> number of elements as "I", first element is "1", and no re-occurring
> values)?
>
> That array I want should be look like this:
>
> >>iWant
>
> =A0 =A0=3D 2 =A0 =A0 6 =A0 =A0 10 =A0 =A0 7 =A0 =A0 3 =A0 =A0 4 =A0 =A0 9=
 =A0 =A0 5 =A0 =A0 8 =A0 =A0 1
>
> Thanks a lot!
>
> -Michael

I think what you want is unique:
[I1,iWant]=3Dunique(I);
which sorts I into I1, rejecting duplicates.
The index of I1 is in iWant.

Subject: sorting array with re-occuring values

From: Michael

Date: 19 Feb, 2009 01:11:17

Message: 10 of 13

On Feb 18, 5:05=A0pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Michael <michael.j.kra...@gmail.com> wrote in message <09fd2fcb-3a08-414a=
-872a-7578408ea...@g3g2000pre.googlegroups.com>...
> > Suppose:
>
> > >> I=3D[3 =A0 =A0 7 =A0 =A010 =A0 =A0 8 =A0 =A0 4 =A0 =A0 5 =A0 =A0 9 =
=A0 =A0 5 =A0 =A0 8 =A0 =A0 2]
>
> > Does anyone know a quick solution how I can use "I" as an index (same
> > number of elements as "I", first element is "1", and no re-occurring
> > values)?
>
> > That array I want should be look like this:
>
> > >>iWant
> > =A0 =A0=3D 2 =A0 =A0 6 =A0 =A0 10 =A0 =A0 7 =A0 =A0 3 =A0 =A0 4 =A0 =A0=
 9 =A0 =A0 5 =A0 =A0 8 =A0 =A0 1
>
> > Thanks a lot!
>
> > -Michael
>
> =A0 It isn't clear from your description, but I suspect you want this:
>
> =A0[t,p] =3D sort(I);
> =A0q =3D 1:size(I,2);
> =A0q(p) =3D q;
>
> In other words you want the inverse of the permutation that is returned b=
y sorting I. =A0The above is an easy way of finding this inverse.
>
> Roger Stafford- Hide quoted text -
>
> - Show quoted text -

This is precisely what I wanted - awesome. Sorry for not being able
to convey this clearly. Thanks a lot for everybody's input and time.

Subject: sorting array with re-occuring values

From: dpb

Date: 19 Feb, 2009 01:16:08

Message: 11 of 13

Michael wrote:
> On Feb 18, 5:05 pm, "Roger Stafford"
...
>> It isn't clear from your description, but I suspect you want...
>> ...the inverse of the permutation that is returned by sorting I.
...
> This is precisely what I wanted - awesome. ...

Damn, that is a helluva a crystal ball, Roger!!! Do you sell those?

(Would beat the Nikes/handbags/etc. all to heck. :) )

--

Subject: sorting array with re-occuring values

From: Matt Fig

Date: 19 Feb, 2009 03:26:02

Message: 12 of 13

It has been my experience that when there is a need for a mathematical crystal ball, Roger is the man to contact.






xbWafTT2WSqqqaqSS7[f[Yb,Zq SgS__q]faagxk`UhSaZqW`ZSa^K?W^Uq

Subject: sorting array with re-occuring values

From: Peter Boettcher

Date: 19 Feb, 2009 14:08:11

Message: 13 of 13


Michael <michael.j.krause@gmail.com> writes:

> On Feb 18, 4:29 pm, "Matt Fig" <spama...@yahoo.com> wrote:
>> Michael <michael.j.kra...@gmail.com> wrote in message
>> <09fd2fcb-3a08-414a-872a-7578408ea...@g3g2000pre.googlegroups.com>...
>> > Suppose:
>>
>> > >> I=[3     7    10     8     4     5     9     5     8     2]
>>
>> > Does anyone know a quick solution how I can use "I" as an index (same
>> > number of elements as "I", first element is "1", and no re-occurring
>> > values)?
>>
>> > That array I want should be look like this:
>>
>> > >>iWant
>> >    = 2     6     10     7     3     4     9     5     8     1
>>
>>
>> I don't see the connection between I and iWant.  Especially when you
>> say that the first element should be 1, then you show the first
>> element as 2, and nothing looks sorted to me at all.  What exactly is
>> the mapping you are after?

[snip Matt's irritating random character string]

> Sorry for the confusion. I try to disambiguate. The smallest element
> in "I" is "2". In "iWant" I set that to "1". The next element in "I"
> is "3", I set that to "2" in "iWant", and so on. The first reoccuring
> element in "I" is "5" (I(8)). I set that to "5" in "iWant" (the first
> "5" was set to "4").
>
> "iWant" is an index of "I". Hope things are more clear now.


Use the optional output of sort to find the indices. Then use that to
generate your iWant:

iWant = zeros(size(I));
[dummy idx] = sort(I);
iWant(idx) = 1:10;


-Peter

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