Got Questions? Get Answers.
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:
Second and third maximum

Subject: Second and third maximum

From: hosam

Date: 19 May, 2011 13:41:04

Message: 1 of 4

Hello,
i need to create a code that extracts the second ,third, and ninth maximum values of a 2D matrix whose size is 1000*1000 and then get their indices as well.
is there a direct function that performs this action.
thanks for your help
Best Regards,
Hossam

Subject: Second and third maximum

From: Wayne King

Date: 19 May, 2011 14:06:02

Message: 2 of 4

"hosam " <hoseng_84@yahoo.com> wrote in message <ir36lg$lv9$1@newscl01ah.mathworks.com>...
> Hello,
> i need to create a code that extracts the second ,third, and ninth maximum values of a 2D matrix whose size is 1000*1000 and then get their indices as well.
> is there a direct function that performs this action.
> thanks for your help
> Best Regards,
> Hossam

One way:

indz = [2 3 9];
X = randn(1e3,1e3);
Y = reshape(X,1e6,1);
[vals,ind] = sort(Y,'descend');
% values
vals(indz)
%indices
[rowindex,columnindex] = ind2sub([1e3 1e3],ind(indz));


Wayne

Subject: Second and third maximum

From: dpb

Date: 19 May, 2011 14:14:00

Message: 3 of 4

On 5/19/2011 8:41 AM, hosam wrote:
> Hello,
> i need to create a code that extracts the second ,third, and ninth
> maximum values of a 2D matrix whose size is 1000*1000 and then get their
> indices as well.
> is there a direct function that performs this action.
> thanks for your help
> Best Regards,
> Hossam

Not aware of directly, no...

multi-output version of sort() will allow you to find the locations
desired by brute force, of course. A 1M vector shouldn't take _too_ long...

 >> x=randn(1000);
 >> tic;[y,idx]=sort(-x(:));toc
elapsed_time =
     0.4060
 >> x(idx(1))==max(x(:))
ans =
      1
 >>

You can wrap in a function to return the desired stuff as a vector for
convenience...

One could do tricks like selecting the values greater than some multiple
of the average to have fewer for sorting at the cost of the selection
and complexity of keeping the indirect addressing; whether it could pay
I couldn't guess w/o trying.

There may be something more clever; doesn't come to me at moment, though.

--

Subject: Second and third maximum

From: Florin Neacsu

Date: 19 May, 2011 15:55:05

Message: 4 of 4

"hosam " <hoseng_84@yahoo.com> wrote in message <ir36lg$lv9$1@newscl01ah.mathworks.com>...
> Hello,
> i need to create a code that extracts the second ,third, and ninth maximum values of a 2D matrix whose size is 1000*1000 and then get their indices as well.
> is there a direct function that performs this action.
> thanks for your help
> Best Regards,
> Hossam

Hi,

You might want to take a look at Bruno's function http://www.mathworks.com/matlabcentral/fileexchange/23576-minmax-selection. I only tested it briefly (some time ago) so I don't remember if it provides indices as well, but as far as finding the max/min is a lot faster then sort.

Regards,
Florin

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