Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!k79g2000hse.googlegroups.com!not-for-mail
From:  "J.N." <joao.natali@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Array manipulation question
Date: Tue, 21 Aug 2007 18:45:54 -0000
Organization: http://groups.google.com
Lines: 27
Message-ID: <1187721954.988507.43000@k79g2000hse.googlegroups.com>
References: <fafah8$ot$1@fred.mathworks.com>
NNTP-Posting-Host: 128.238.52.136
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Trace: posting.google.com 1187721957 2110 127.0.0.1 (21 Aug 2007 18:45:57 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 21 Aug 2007 18:45:57 +0000 (UTC)
In-Reply-To: <fafah8$ot$1@fred.mathworks.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: k79g2000hse.googlegroups.com; posting-host=128.238.52.136;
Xref: news.mathworks.com comp.soft-sys.matlab:424826



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.