Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding the N maximum values in a matrix

Subject: Finding the N maximum values in a matrix

From: Mohammad Nasr

Date: 5 Aug, 2009 11:45:04

Message: 1 of 9

Hi

Is there any way to find n maximum/minimum values in a matrix without going through the for loop.

let's say we are looking for finding n=4 maximum number from following matrix
A = [7 1 19;
      16 13 1;
      14 8 10]

I am looking for finding the triplets of [r,c,v] as following:
[1,3,19] -> 1st maximum value
[2,1,16] -> 2nd maximum value
[3,1,14] -> 3rd maximum value
[3,3,10] -> 4th maximum value

in [r,c,v] triplet, [r,c] indicate the row and column position of the n-th maximum value and v is it's value.


due to the very huge size of matrix A (like 10000*10000) and large n (like 100) I really want to avoid going through loops in matlab.


Thanks a lot in advance

-Mohammad

Subject: Finding the N maximum values in a matrix

From: us

Date: 5 Aug, 2009 11:54:01

Message: 2 of 9

"Mohammad Nasr" <mohammad.nasr@gmail.com> wrote in message <h5brc0$7qs$1@fred.mathworks.com>...
> Hi
>
> Is there any way to find n maximum/minimum values in a matrix without going through the for loop.
>
> let's say we are looking for finding n=4 maximum number from following matrix
> A = [7 1 19;
> 16 13 1;
> 14 8 10]
>
> I am looking for finding the triplets of [r,c,v] as following:
> [1,3,19] -> 1st maximum value
> [2,1,16] -> 2nd maximum value
> [3,1,14] -> 3rd maximum value
> [3,3,10] -> 4th maximum value
>
> in [r,c,v] triplet, [r,c] indicate the row and column position of the n-th maximum value and v is it's value.
>
>
> due to the very huge size of matrix A (like 10000*10000) and large n (like 100) I really want to avoid going through loops in matlab.
>
>
> Thanks a lot in advance
>
> -Mohammad

one of the many solutions

% the data
     n=4;
     m=[
          7,1,19
          16,13,1
          14,8,10
     ];
% the engine
     [ms,mx]=sort(m(:),'descend');
     [rx,cx]=ind2sub(size(m),mx);
     r=[rx,cx,ms];
     r=r(1:n,:);
% the result
     disp(r);
%{
% row# col# val
          1 3 19
          2 1 16
          3 1 14
          2 2 13
%}

us

Subject: Finding the N maximum values in a matrix

From: Bruno Luong

Date: 5 Aug, 2009 12:31:02

Message: 3 of 9

"Mohammad Nasr" <mohammad.nasr@gmail.com> wrote in message <h5brc0$7qs$1@fred.mathworks.com>...
> Hi
>
> Is there any way to find n maximum/minimum values in a matrix without going through the for loop.
>
> let's say we are looking for finding n=4 maximum number from following matrix
> A = [7 1 19;
> 16 13 1;
> 14 8 10]
>
> I am looking for finding the triplets of [r,c,v] as following:
> [1,3,19] -> 1st maximum value
> [2,1,16] -> 2nd maximum value
> [3,1,14] -> 3rd maximum value
> [3,3,10] -> 4th maximum value
>
> in [r,c,v] triplet, [r,c] indicate the row and column position of the n-th maximum value and v is it's value.
>
>
> due to the very huge size of matrix A (like 10000*10000) and large n (like 100) I really want to avoid going through loops in matlab.
>
>
> Thanks a lot in advance
>
> -Mohammad

This is what you are looking for.
http://www.mathworks.com/matlabcentral/fileexchange/23576

Bruno

Subject: Finding the N maximum values in a matrix

From: Mohammad Nasr

Date: 5 Aug, 2009 13:56:02

Message: 4 of 9

"us " <us@neurol.unizh.ch> wrote in message <h5brsp$cfu$1@fred.mathworks.com>...
> "Mohammad Nasr" <mohammad.nasr@gmail.com> wrote in message <h5brc0$7qs$1@fred.mathworks.com>...
> > Hi
> >
> > Is there any way to find n maximum/minimum values in a matrix without going through the for loop.
> >
> > let's say we are looking for finding n=4 maximum number from following matrix
> > A = [7 1 19;
> > 16 13 1;
> > 14 8 10]
> >
> > I am looking for finding the triplets of [r,c,v] as following:
> > [1,3,19] -> 1st maximum value
> > [2,1,16] -> 2nd maximum value
> > [3,1,14] -> 3rd maximum value
> > [3,3,10] -> 4th maximum value
> >
> > in [r,c,v] triplet, [r,c] indicate the row and column position of the n-th maximum value and v is it's value.
> >
> >
> > due to the very huge size of matrix A (like 10000*10000) and large n (like 100) I really want to avoid going through loops in matlab.
> >
> >
> > Thanks a lot in advance
> >
> > -Mohammad
>
> one of the many solutions
>
> % the data
> n=4;
> m=[
> 7,1,19
> 16,13,1
> 14,8,10
> ];
> % the engine
> [ms,mx]=sort(m(:),'descend');
> [rx,cx]=ind2sub(size(m),mx);
> r=[rx,cx,ms];
> r=r(1:n,:);
> % the result
> disp(r);
> %{
> % row# col# val
> 1 3 19
> 2 1 16
> 3 1 14
> 2 2 13
> %}
>
> us

thanks but I guess sorting that huge matrix to find relatively very small number of values is not saving time complexity

Subject: Finding the N maximum values in a matrix

From: Mohammad Nasr

Date: 5 Aug, 2009 13:58:02

Message: 5 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5bu26$5r5$1@fred.mathworks.com>...
> "Mohammad Nasr" <mohammad.nasr@gmail.com> wrote in message <h5brc0$7qs$1@fred.mathworks.com>...
> > Hi
> >
> > Is there any way to find n maximum/minimum values in a matrix without going through the for loop.
> >
> > let's say we are looking for finding n=4 maximum number from following matrix
> > A = [7 1 19;
> > 16 13 1;
> > 14 8 10]
> >
> > I am looking for finding the triplets of [r,c,v] as following:
> > [1,3,19] -> 1st maximum value
> > [2,1,16] -> 2nd maximum value
> > [3,1,14] -> 3rd maximum value
> > [3,3,10] -> 4th maximum value
> >
> > in [r,c,v] triplet, [r,c] indicate the row and column position of the n-th maximum value and v is it's value.
> >
> >
> > due to the very huge size of matrix A (like 10000*10000) and large n (like 100) I really want to avoid going through loops in matlab.
> >
> >
> > Thanks a lot in advance
> >
> > -Mohammad
>
> This is what you are looking for.
> http://www.mathworks.com/matlabcentral/fileexchange/23576
>
> Bruno


thanks a lot Bruno
quick sort is not a bad idea.

Subject: Finding the N maximum values in a matrix

From: Bruno Luong

Date: 5 Aug, 2009 15:19:02

Message: 6 of 9

"Mohammad Nasr" <mohammad.nasr@gmail.com> wrote in message <h5c35a$m3h$1@fred.mathworks.com>...

>
> thanks a lot Bruno
> quick sort is not a bad idea.

Sorry to be pedantic, but it's *Partial* quicksort, which is not the same thing. In any case it will do the job in an optimal way.

Bruno

Subject: Finding the N maximum values in a matrix

From: dpb

Date: 5 Aug, 2009 17:23:07

Message: 7 of 9

Bruno Luong wrote:
> "Mohammad Nasr" <mohammad.nasr@gmail.com> wrote in message
> <h5c35a$m3h$1@fred.mathworks.com>...
>
>> thanks a lot Bruno quick sort is not a bad idea.
>
> Sorry to be pedantic, but it's *Partial* quicksort, which is not the
> same thing. In any case it will do the job in an optimal way.

What about the max() by row, then order that vector?

--

Subject: Finding the N maximum values in a matrix

From: us

Date: 5 Aug, 2009 17:37:18

Message: 8 of 9

dpb <none@non.net> wrote in message <h5cfrp$u6o$1@news.eternal-september.org>...
> Bruno Luong wrote:
> > "Mohammad Nasr" <mohammad.nasr@gmail.com> wrote in message
> > <h5c35a$m3h$1@fred.mathworks.com>...
> >
> >> thanks a lot Bruno quick sort is not a bad idea.
> >
> > Sorry to be pedantic, but it's *Partial* quicksort, which is not the
> > same thing. In any case it will do the job in an optimal way.
>
> What about the max() by row, then order that vector?
>
> --

but - this would not work for, eg,

     m=[
          7,1,19
          16,13,1
          14,8,10
     ];
     r=max(m,[],2).'
% r = 19 16 14 % <- however, the OP wants 4 maxs - the 13 is missing...

us

Subject: Finding the N maximum values in a matrix

From: dpb

Date: 5 Aug, 2009 17:45:51

Message: 9 of 9

us wrote:
> dpb <none@non.net> wrote in message <h5cfrp$u6o$1@news.eternal-september.org>...
...

>> What about the max() by row, then order that vector?
>>
>> --
>
> but - this would not work for, eg,
>
> m=[
> 7,1,19
> 16,13,1
> 14,8,10
> ];
> r=max(m,[],2).'
> % r = 19 16 14 % <- however, the OP wants 4 maxs - the 13 is missing...

Yeah, it falls down doesn't it...oh well, just a thought (I didn't say
it was a _good_ thought... :) )

--

Tags for this Thread

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.

Contact us