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:
problem finding a value

Subject: problem finding a value

From: Iris

Date: 21 Jan, 2013 15:39:08

Message: 1 of 4

hello!
i have a vector like this:

x_x =

  1.0e+002 *

  Columns 1 through 3

   1.000000000000000 -0.953406571178785 -1.542839603993645

  Columns 4 through 5

  -2.146721794700071 -3.268216911140570

now i say:
if any(x_x < 2.5) && any(x_x > -5)
     do something: A
else
     do something else: B
end

why does the program always do A??
i just want it to do A if some value inside x_x is between -5 and 2.5!
i really dont understand....

any help is appreciated..
thank you in advance

Subject: problem finding a value

From: Josh Meyer

Date: 21 Jan, 2013 15:50:36

Message: 2 of 4

The 'any' operator is true if any element of a vector is a nonzero number or
is logical 1 (TRUE). As written, your loop tests the entire array and not
each element, so the result is always 'true' since x_x contains an element
that is <2.5 and also one that is >-5.

To fix:

for i=1:numel(x)
if x_x(i) < 2.5 && x_x(i) > -5
    do something: A
else
    do something else: B
end
end
"Iris " <my.velvet.faerie.tale@gmail.com> wrote in message
news:kdjnes$5ii$1@newscl01ah.mathworks.com...
> hello!
> i have a vector like this:
>
> x_x =
>
> 1.0e+002 *
>
> Columns 1 through 3
>
> 1.000000000000000 -0.953406571178785 -1.542839603993645
>
> Columns 4 through 5
>
> -2.146721794700071 -3.268216911140570
>
> now i say:
> if any(x_x < 2.5) && any(x_x > -5) do something: A
> else do something else: B
> end
>
> why does the program always do A??
> i just want it to do A if some value inside x_x is between -5 and 2.5!
> i really dont understand....
>
> any help is appreciated..
> thank you in advance

Subject: problem finding a value

From: Iris

Date: 21 Jan, 2013 16:06:08

Message: 3 of 4

Hello Josh, thank you for your answer!
The problem stands...
imagine that:
x_x =

  1.0e+002 *

  Columns 1 through 3

   1.000000000000000 -0.953406571178785 -1.542839603993645

  Columns 4 through 5

  0.02146721794700071 -3.268216911140570

the 4th (for i = 4) value is inside the range that i want, but then it continues for i=5 which is not inside the range... so it will do B...
how can i do this?
thank u *

Subject: problem finding a value

From: dpb

Date: 21 Jan, 2013 19:26:36

Message: 4 of 4

On 1/21/2013 10:06 AM, Iris wrote:
> Hello Josh, thank you for your answer!
> The problem stands...
> imagine that:
> x_x =
>
...

> 100.000 -95.340657 -154.28396 2.14672 -326.82169

Fixing format to be legible...

> the 4th (for i = 4) value is inside the range that i want, but then it
> continues for i=5 which is not inside the range... so it will do B...
> how can i do this?

Well, what would be the problem as written (altho I'd likely use CASE
instead of IF here...)

 >> for i=1:length(x_x)
if (x_x(i)>-5) & (x_x(i)<2.5)
disp('A')
else
disp('B')
end,end
B
B
B
A
B
 >>

Looks like exactly what you asked for to me...

--

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