Thread Subject: Array manipulation question

Subject: Array manipulation question

From: Adam

Date: 21 Aug, 2007 18:22:32

Message: 1 of 8

Hi everyone,
   I'm looking for a way to have Matlab return some number
(N) of the smallest elements in an array along with their
indices in the array. For example, given the array:

[5 4 3 2 1] and N = 3

The output should be something like:

[[3 3],[2 4],[1 5]]

This can be done with brute force but I'm looking for a
cleaner implementation. I can't use the sort function
because it re-orders the array and I would lose the index
information. Does matlab support hash tables? Any other
ideas? Thanks.

Adam

Subject: Array manipulation question

From: Peter Boettcher

Date: 21 Aug, 2007 18:36:56

Message: 2 of 8

"Adam " <crossroads_evh.nospam@hotmail.com> writes:

> Hi everyone,
> I'm looking for a way to have Matlab return some number
> (N) of the smallest elements in an array along with their
> indices in the array. For example, given the array:
>
> [5 4 3 2 1] and N = 3
>
> The output should be something like:
>
> [[3 3],[2 4],[1 5]]
>
> This can be done with brute force but I'm looking for a
> cleaner implementation. I can't use the sort function
> because it re-orders the array and I would lose the index
> information. Does matlab support hash tables? Any other
> ideas? Thanks.

help sort:

    [Y,I] = SORT(X,DIM,MODE) also returns an index matrix I.
    If X is a vector, then Y = X(I).
    If X is an m-by-n matrix and DIM=1, then
        for j = 1:n, Y(:,j) = X(I(:,j),j); end
 

-Peter

Subject: Array manipulation question

From: us

Date: 21 Aug, 2007 18:38:23

Message: 3 of 8

Adam:
<SNIP error of <sort>s...

> I can't use the sort function because it re-orders the
array and I would lose the index information...

why?

     v=[3,4,1,2,5].';
     [vs,vx]=sort(v);
     n=3;
     r=[vs(1:n),vx(1:n)];
     disp(r);

also, look at <loren shure>'s recent hootenanny of <sort>s

http://blogs.mathworks.com/loren/2007/08/21/reversal-of-a-
sort/

to sort things out...

us

Subject: Array manipulation question

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Aug, 2007 18:45:15

Message: 4 of 8

In article <fafah8$ot$1@fred.mathworks.com>,
Adam <crossroads_evh.nospam@hotmail.com> wrote:
> I'm looking for a way to have Matlab return some number
>(N) of the smallest elements in an array along with their
>indices in the array. For example, given the array:

>[5 4 3 2 1] and N = 3

>The output should be something like:
>
>[[3 3],[2 4],[1 5]]

>This can be done with brute force but I'm looking for a
>cleaner implementation. I can't use the sort function
>because it re-orders the array and I would lose the index
>information.

A = [5 4 3 2 1];
N = 3;

[sortedvec, sortidx] = sort(A);
origorder = sort(sortidx(1:N));
result = [A(origorder);origorder]'

result =

     3 3
     2 4
     1 5

--
  There are some ideas so wrong that only a very intelligent person
  could believe in them. -- George Orwell

Subject: Array manipulation question

From: "J.N.

Date: 21 Aug, 2007 18:45:54

Message: 5 of 8

On Aug 21, 2:22 pm, "Adam " <crossroads_evh.nos...@hotmail.com> wrote:
> Hi everyone,
> I'm looking for a way to have Matlab return some number
> (N) of the smallest elements in an array along with their
> indices in the array. For example, given the array:
>
> [5 4 3 2 1] and N = 3
>
> The output should be something like:
>
> [[3 3],[2 4],[1 5]]
>
> This can be done with brute force but I'm looking for a
> cleaner implementation. I can't use the sort function
> because it re-orders the array and I would lose the index
> information. Does matlab support hash tables? Any other
> ideas? Thanks.
>
> Adam

The syntax:
[As,Ix] = sort(A)
returns in Ix the indexes in A of the sorted elements in As. Picking
the first N elements of the pair As and Ix should do what you want.

J.N.

Subject: Array manipulation question

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Aug, 2007 18:50:10

Message: 6 of 8

In article <fafbev$dku$1@fred.mathworks.com>, us <us@neurol.unizh.ch> wrote:
>Adam:
><SNIP error of <sort>s...

>> I can't use the sort function because it re-orders the
>array and I would lose the index information...

> v=[3,4,1,2,5].';
> [vs,vx]=sort(v);
> n=3;
> r=[vs(1:n),vx(1:n)];

That will give the values in sorted order, but Adam wants the values
in the original order (for whatever reason). See my posting for
the (small) change needed to get the original order.
--
   Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

Subject: Array manipulation question

From: Adam

Date: 21 Aug, 2007 19:44:23

Message: 7 of 8

My mistake! The sort function works fine. I'll have to
read the help file more closely next time. Thanks for the
replies.

Subject: Array manipulation question

From: us

Date: 21 Aug, 2007 20:43:10

Message: 8 of 8

Walter Roberson:
<SNIP scolding a flawed solutions...

> Adam wants the values in the original order (for whatever
reason)...

hmmmm, he/she didn't say so explicitly - the (unfortunate?)
example could just have been a descending sort - naturally,
i assume you are correct...

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 21 Aug, 2007 14:40:23
sort us 21 Aug, 2007 14:40:23
reference us 21 Aug, 2007 14:40:23
rssFeed for this Thread

Contact us at files@mathworks.com