Thread Subject: Top N values in a multidimensional array

Subject: Top N values in a multidimensional array

From: David Doria

Date: 28 Oct, 2008 19:43:02

Message: 1 of 5

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

Subject: Top N values in a multidimensional array

From: First Last

Date: 28 Oct, 2008 20:28:02

Message: 2 of 5

"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.

Subject: Top N values in a multidimensional array

From: David Doria

Date: 29 Oct, 2008 13:20:03

Message: 3 of 5

Hmm it seems like that returns a scalar when I would have liked a vector of the 3 coordinates - ie (4, 5, 2) where that just says 51.

Thanks,
Dave

Subject: Top N values in a multidimensional array

From: David Doria

Date: 29 Oct, 2008 15:14:02

Message: 4 of 5

I see, you actually have to give it 3 output values

[i j k] = ind2sub();

I thought it would just make 'output' a 1x3 vector with
output = ind2sub();

but I was wrong.

Thanks,
Dave

Subject: Top N values in a multidimensional array

From: Wolfgang Schwanghart

Date: 29 Oct, 2008 18:46:03

Message: 5 of 5

or just...

n = 2; %number of max values

[a,ix] = sort(A(:),'descend');
a = a(1:n);
ix = ix(1:n);

than use ind2sub to find the subscripts.

HTH,
Wolfgang

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com