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 6

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 6

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

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 6

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 6

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

Subject: Top N values in a multidimensional array

From: Natasha

Date: 18 Nov, 2010 16:19:04

Message: 6 of 6

"Wolfgang Schwanghart" <schwanghart@googlemail.com> wrote in message <geab1b$1ad$1@fred.mathworks.com>...
> 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


Thanks Wolfgang, that's a nice bit of code.
Natasha

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

Contact us at files@mathworks.com