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:
Getting Top 'k' most negative value indices in matrix

Subject: Getting Top 'k' most negative value indices in matrix

From: Rahul Singhal

Date: 6 Jul, 2010 16:55:20

Message: 1 of 7

Hi All,
  I have a matrix which contains some negative values. I want to get the indices of top 'k' most negative values in matrix.

How can i do that?

Thanks and Regards
Rahul

Subject: Getting Top 'k' most negative value indices in matrix

From: dpb

Date: 6 Jul, 2010 17:08:36

Message: 2 of 7

Rahul Singhal wrote:
> Hi All,
> I have a matrix which contains some negative values. I want to get the
> indices of top 'k' most negative values in matrix.
> How can i do that?
...

Would that not be the k smallest entries?

If so, the brain-dead way would be

xsort = sort(x(:));
min_k = xsort(1:k);

--

Subject: Getting Top 'k' most negative value indices in matrix

From: Roger Stafford

Date: 6 Jul, 2010 17:13:24

Message: 3 of 7

"Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <i0vn5o$sqt$1@fred.mathworks.com>...
> Hi All,
> I have a matrix which contains some negative values. I want to get the indices of top 'k' most negative values in matrix.
>
> How can i do that?
>
> Thanks and Regards
> Rahul
- - - - - - - - -
  Check out this FEX contribution by Bruno Luong:

 http://www.mathworks.com/matlabcentral/fileexchange/23576-minmax-selection

Roger Stafford

Subject: Getting Top 'k' most negative value indices in matrix

From: Oleg Komarov

Date: 6 Jul, 2010 17:13:24

Message: 4 of 7

"Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <i0vn5o$sqt$1@fred.mathworks.com>...
> Hi All,
> I have a matrix which contains some negative values. I want to get the indices of top 'k' most negative values in matrix.
>
> How can i do that?
>
> Thanks and Regards
> Rahul

k = 9;
A = rand(10,10);
[sortedA, idx] = sort(A(:));
kMostNeg = idx(end-k:end);

Oleg

Subject: Getting Top 'k' most negative value indices in matrix

From: Oleg Komarov

Date: 6 Jul, 2010 17:14:07

Message: 5 of 7

typo, last line should be:
kMostNeg = idx(end-k+1:end);

Oleg

Subject: Getting Top 'k' most negative value indices in matrix

From: Wayne King

Date: 6 Jul, 2010 17:19:07

Message: 6 of 7

"Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <i0vn5o$sqt$1@fred.mathworks.com>...
> Hi All,
> I have a matrix which contains some negative values. I want to get the indices of top 'k' most negative values in matrix.
>
> How can i do that?
>
> Thanks and Regards
> Rahul

Hi Rahul, have you read the documentation for sort?

reset(RandStream.getDefaultStream);
A = randn(10,10);
[B,I] = sort(A(:));
A(I(1:10)) %smallest ten elements I(1:10) are the indices

Is this what you're getting at?

Wayne

Subject: Getting Top 'k' most negative value indices in matrix

From: Rahul Singhal

Date: 7 Jul, 2010 00:40:26

Message: 7 of 7

Thanks Wayne. Yes, my problem is solved by sort technique u mentioned below.
Thanks once again...
Rahul

"Wayne King" <wmkingty@gmail.com> wrote in message <i0voib$1do$1@fred.mathworks.com>...
> "Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <i0vn5o$sqt$1@fred.mathworks.com>...
> > Hi All,
> > I have a matrix which contains some negative values. I want to get the indices of top 'k' most negative values in matrix.
> >
> > How can i do that?
> >
> > Thanks and Regards
> > Rahul
>
> Hi Rahul, have you read the documentation for sort?
>
> reset(RandStream.getDefaultStream);
> A = randn(10,10);
> [B,I] = sort(A(:));
> A(I(1:10)) %smallest ten elements I(1:10) are the indices
>
> Is this what you're getting at?
>
> Wayne

Tags for this Thread

No tags are associated with 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