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:
strfind() problems

Subject: strfind() problems

From: Job de Lange

Date: 14 Oct, 2010 15:21:05

Message: 1 of 4

Hi, I need to use the function strfind(,) in a programme I'm making. My intention is to compare 2 big vectors and I want to get the cell where they have the same value.

Because the vectors are very big I made a simple example:
Let's take:
a=1:6
b=2:9
strfind(a,b(4))
Gives [5] so this works.

But if I take
a=0.1:0.1:3
b=0.3:0.1:4
strfind(a,b(14)
gives [] although the 14th value of b does match the 16th value in a.

So my question is what are the possibilities for the problem that strfind does not work?

Is the problem that vector b is longer than vector a? And if that's the problem is there anyway to go around it?

Subject: strfind() problems

From: Sean

Date: 14 Oct, 2010 15:35:04

Message: 2 of 4

"Job de Lange" <jobbieboy4@hotmail.com> wrote in message <i97751$i9s$1@fred.mathworks.com>...
> Hi, I need to use the function strfind(,) in a programme I'm making. My intention is to compare 2 big vectors and I want to get the cell where they have the same value.
>
> Because the vectors are very big I made a simple example:
> Let's take:
> a=1:6
> b=2:9
> strfind(a,b(4))
> Gives [5] so this works.
>
> But if I take
> a=0.1:0.1:3
> b=0.3:0.1:4
> strfind(a,b(14)
> gives [] although the 14th value of b does match the 16th value in a.
>
> So my question is what are the possibilities for the problem that strfind does not work?
>
> Is the problem that vector b is longer than vector a? And if that's the problem is there anyway to go around it?

First off you should be doing a direct comparison of values, not using strfind. Second, take a peek at FAQ 6.1

http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F

Subject: strfind() problems

From: Job de Lange

Date: 14 Oct, 2010 15:48:04

Message: 3 of 4

Thank You

The problem is what you noted, the numbers aren't exactly equal.

So now the problem is to let matlab check if the first 3 decimal places are the same.

Is there any code for it in matlab?

For examle in google you can use anim* and you wil find animal is there a sign in matlab that works the same or is there another sollution?

thanks again

Subject: strfind() problems

From: Sean

Date: 14 Oct, 2010 15:55:05

Message: 4 of 4

"Job de Lange" <jobbieboy4@hotmail.com> wrote in message <i978nk$46t$1@fred.mathworks.com>...
> Thank You
>
> The problem is what you noted, the numbers aren't exactly equal.
>
> So now the problem is to let matlab check if the first 3 decimal places are the same.
>
> Is there any code for it in matlab?

If the absolute difference of the two numbers is less than 1/1000 then they are equal by your aforementioned criterion.

You could also do this more sloppily by multiplying them by a thousand, converting to integers, and then testing equality.

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