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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

If Condition match but got the else statement ?

Asked by I Made on 27 Mar 2013

To put it simple my code similar to :

    k=6
    if(a/b<k) && (a/b>=0)
     STATEMENT1
    elseif(a/b>-k) && (a/b<0)
     STATEMENT2
    end

The problem is i got a/b = 0, so it should give me STATEMENT1 but i got STATEMENT 2, does anyone know how i can figure out my mistake?

3 Comments

Cedric Wannaz on 27 Mar 2013

Did you try to check that a/b is really close enough to 0?

 >> [a/b == 0, a/b > 0, a/b < 0]

what does that return ?

I Made on 27 Mar 2013

i'm trying using that but little modification, like :

   posisi_eks(l,4)=segment(j,i)/c(i)==0;

on the poin that segment(j,i) is 0 it return me 1 . but how could that possibly done? i checked the segment(j,i) at that point = 0, and the c(i) at that pint is 0,9375. So the result should be 0/0.9375==0 return me 0?

Walter Roberson on 28 Mar 2013

Please try

fprintf('%.100g\n', segment(j,i))
I Made

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson on 27 Mar 2013

Try using the command

format long g

and then displaying a/b

It probably isn't really 0.

0 Comments

Walter Roberson
Answer by Jan Simon on 28 Mar 2013

Please read your question again and note, that Matlab does not perform any strange or magic actions. Then these two details:

 1. if(a/b<k) && (a/b>=0)
 2. The problem is i got a/b = 0, so it should give me STATEMENT1 but i got STATEMENT 2

imply, that either a/b<k is FALSE, or your assumption that a/b==0 is wrong. A third possibility is, that you did not save the M-file after editing, such that Matlab does not run the program you currently see in the editor.

There are no other solutions. Walter's suggestion to increase the displayed precision is exhaustive to identify the problems.

0 Comments

Jan Simon

Contact us