Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14352
| From | Fred Simons <f.h.simons@tue.nl> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: sorting problem |
| Date | 2013-04-28 09:17 +0000 |
| Message-ID | <klipem$11k$1@smc.vnet.net> (permalink) |
| References | <20130428045759.9057869D1@smc.vnet.net> |
| Organization | Time-Warner Telecom |
Here is one solution, assuming that all elements in your list distances
are non-negative:
In[1]:= distances={0,Sqrt[2],Sqrt[2],0,0,Sqrt[2],2,Sqrt[2]};
In[2]:= Table[Position[distances,x,1],{x, Union[distances]}] // Flatten
Out[2]= {1,4,5,7,2,3,6,8}
Regards,
Fred Simons
Eindhoven University of Technology
Op 28-4-2013 6:57, S schreef:
> Hello
>
> I need to sort a list in increasing magnitude. If two elements in the list are the same, the element which appears EARLIER in the list should appear first. Finally, I need the indices of the sorted list according to the above two rules.
>
> e.g. for input
> distances = {0, Sqrt[2], Sqrt[2], 0, 0, Sqrt[2], 2, Sqrt[2]}
> I need the answer to be
> k={1,4,5,2,3,6,8,7}
>
> I tried the following commands:
>
> distances = {0, Sqrt[2], Sqrt[2], 0, 0, Sqrt[2], 2, Sqrt[2]}
> Sorteddistances = Sort[distances, Less]
> k = Ordering[distances]
>
> I get output k={1, 4, 5, 7, 2, 3, 6, 8}.
>
> However, I want the output k={1,4,5,2,3,6,8,7}
>
> Can someone please suggest how to do this.
>
> Thanks
>
> S
>
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: sorting problem Fred Simons <f.h.simons@tue.nl> - 2013-04-28 09:17 +0000
csiph-web