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:
Exctract indices of certain values from a vector

Subject: Exctract indices of certain values from a vector

From: Tim Franke

Date: 21 Jul, 2012 18:10:20

Message: 1 of 5

Hi,

I have a column vector that looks like this:

0.0
0.1
0.2
0.3
0.4
0.0
0.1
0.2
0.3
0.4
0.0
0.1

I want to get a new vector that contains the indices of e.g. "0.1".
So in this example the vector shout be [2 7 12].
I tried with the "find" function, but I did not succeed.

Can somebody give a hint?

Best regards,
Tim

Subject: Exctract indices of certain values from a vector

From: dpb

Date: 21 Jul, 2012 21:21:50

Message: 2 of 5

On 7/21/2012 1:10 PM, Tim Franke wrote:
...

> 0.0
> 0.1
...
> 0.0
> 0.1
>
> I want to get a new vector that contains the indices of e.g. "0.1".
> So in this example the vector shout be [2 7 12].
> I tried with the "find" function, but I did not succeed.
...

What did you try w/ find() specifically?

There's always the problem w/ floating point comparisons that some
values (such as 0.1) are not _exactly_ representable and so depending on
how the values were generated the result of what looks superficially
like "0.1==0.1" can return false.

See FAQ

<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>

If this turns out to be the problem there's at least one solution at the
File Exchange (altho I don' have a direct link handy) or you can write a
test expression explicitly.

--

Subject: Exctract indices of certain values from a vector

From: Bruno Luong

Date: 21 Jul, 2012 21:25:18

Message: 3 of 5

find(abs(a-0.1)<=1e-10)

Bruno

Subject: Exctract indices of certain values from a vector

From: steve.nospamm@gmail.com

Date: 22 Jul, 2012 04:00:30

Message: 4 of 5

On Saturday, July 21, 2012 1:10:20 PM UTC-5, Tim Franke wrote:
> Hi,
>
> I have a column vector that looks like this:
>
> 0.0
> 0.1
> 0.2
> 0.3
> 0.4
> 0.0
> 0.1
> 0.2
> 0.3
> 0.4
> 0.0
> 0.1
>
> I want to get a new vector that contains the indices of e.g. "0.1".
> So in this example the vector shout be [2 7 12].
> I tried with the "find" function, but I did not succeed.
>
> Can somebody give a hint?
>
> Best regards,
> Tim

use the loop for like this

for ii=1: length(x)
  if x(ii)=0.1 then
     disp(ii);
  end
end

this show all index of vector for x=0.1

Subject: Exctract indices of certain values from a vector

From: dpb

Date: 22 Jul, 2012 04:47:24

Message: 5 of 5

On 7/21/2012 11:00 PM, steve.nospamm@gmail.com wrote:
> On Saturday, July 21, 2012 1:10:20 PM UTC-5, Tim Franke wrote:
>> Hi,
>>
>> I have a column vector that looks like this:
>>
>> 0.0
>> 0.1
>> 0.2
>> 0.3
>> 0.4
>> 0.0
>> 0.1
>> 0.2
>> 0.3
>> 0.4
>> 0.0
>> 0.1
>>
>> I want to get a new vector that contains the indices of e.g."0.1".
>> So in this example the vector shout be [2 7 12].
>> I tried with the"find" function, but I did not succeed.
>>
>> Can somebody give a hint?
>>
>> Best regards,
>> Tim
>
> use the loop for like this
>
> for ii=1: length(x)
> if x(ii)=0.1 then
> disp(ii);
> end
> end
>
> this show all index of vector for x=0.1

Two problems w/ that --

a) if x(ii)=0.1 then

is a syntax error; '=' --> '=='

b) a) will suffer from the same floating point exact comparison problem
as OP had w/ find()

--

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