Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Top N values in a multidimensional array
Date: Tue, 28 Oct 2008 20:28:02 +0000 (UTC)
Organization: Boston University
Lines: 26
Message-ID: <ge7ski$aml$1@fred.mathworks.com>
References: <ge7q06$4k2$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1225225682 10965 172.30.248.37 (28 Oct 2008 20:28:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 28 Oct 2008 20:28:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 844160
Xref: news.mathworks.com comp.soft-sys.matlab:497698


"David Doria" <daviddoria@gmail.com> wrote in message <ge7q06$4k2$1@fred.mathworks.com>...
> I didn't see anything like on the file exchange, which really surprised me. 
> 
> I have a multidimensional array that I would like to query for the largest N values.  ie if i say
> 
> [v loc] = max(A, 2);
> 
> where A is 3x3x10, I would like v to be length 2 containing the largest two values, and loc to be length 2 containing the positions of those values (a coordinate in the nXmXp space).
> 
> Does someone know of a script like this that I just missed on the file exchange? It just seems like it would be terribly inefficient if I wrote it.
> 
> Thanks,
> 
> Dave

Hi Dave,

Just one of many solutions could look like (with A as you defined it):

A_col = reshape(A,[],1);
A_sort = sort(A_col,'descend');
v = A_sort(1:2);
lin_loc = find(A_col == v(1)); % for the first max
real_loc = ind2sub(size(A),lin_loc); % for the first max

Playing around with that will allow you to customize it.