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:
Getting index of value closest to, but larger than zero

Subject: Getting index of value closest to, but larger than zero

From: Sargondjani

Date: 29 Aug, 2011 10:16:26

Message: 1 of 4

If one has a vector A and wants to find the index of the value closest to, but larger than zero. I thought this would do the trick:

[value,index]=min(A(A>0))

but this gives the index of the vector A(A>0) (which has a different size).

How can I change it so i get the index in the matrix A?
(there is an easier solution than getting the value of min(A(A>0)) and than looking up this value in A, right?)

Subject: Getting index of value closest to, but larger than zero

From: Bruno Luong

Date: 29 Aug, 2011 11:00:29

Message: 2 of 4

Just use cascading indexing:

>> A=randn(1,10)

A =

  Columns 1 through 8

   -1.3499 3.0349 0.7254 -0.0631 0.7147 -0.2050 -0.1241 1.4897

  Columns 9 through 10

    1.4090 1.4172

>> ipos = find(A>0)

ipos =

     2 3 5 8 9 10

>> [value,imin]=min(A(ipos))

value =

    0.7147


imin =

     3

>> imin=ipos(imin)

imin =

     5

>> A(imin)

ans =

    0.7147

% Bruno

Subject: Getting index of value closest to, but larger than zero

From: Krishna Kumar

Date: 29 Aug, 2011 11:03:26

Message: 3 of 4

"Sargondjani" wrote in message <j3fotq$6na$1@newscl01ah.mathworks.com>...
> If one has a vector A and wants to find the index of the value closest to, but larger than zero. I thought this would do the trick:
>
> [value,index]=min(A(A>0))
>
> but this gives the index of the vector A(A>0) (which has a different size).
>
> How can I change it so i get the index in the matrix A?
> (there is an easier solution than getting the value of min(A(A>0)) and than looking up this value in A, right?)

Try using find command...

Subject: Getting index of value closest to, but larger than zero

From: Sargondjani

Date: 29 Aug, 2011 11:35:14

Message: 4 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j3frgd$dd9$1@newscl01ah.mathworks.com>...
> Just use cascading indexing:
>
> >> A=randn(1,10)
>
> A =
>
> Columns 1 through 8
>
> -1.3499 3.0349 0.7254 -0.0631 0.7147 -0.2050 -0.1241 1.4897
>
> Columns 9 through 10
>
> 1.4090 1.4172
>
> >> ipos = find(A>0)
>
> ipos =
>
> 2 3 5 8 9 10
>
> >> [value,imin]=min(A(ipos))
>
> value =
>
> 0.7147
>
>
> imin =
>
> 3
>
> >> imin=ipos(imin)
>
> imin =
>
> 5
>
> >> A(imin)
>
> ans =
>
> 0.7147
>
> % Bruno

Thanks Bruno!! That's a pretty clever way to solve it :-D
I was hoping it would be possible in less lines, it seemed such an easy thing... Anyway, it works! :-)

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