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:
if loop condition

Subject: if loop condition

From: prabhakaran m

Date: 20 May, 2012 02:50:22

Message: 1 of 5


i want to use this condition to find f(x)
f(x) = 1 + 2x for x between -1 and 0 and f(x) = 1 - 2x for x between 0 and 1

clear all
N=41;
x=linspace(-1,1,41)
theta=-pi:2*pi/(N-1):pi;

if x >-1 & x<=0
    y=1+2*x
else
    y=1-2*x
end

i am getting the y values wrongly. what is the mistake i am doing. pls correct me

Subject: if loop condition

From: Roger Stafford

Date: 20 May, 2012 06:08:44

Message: 2 of 5

"prabhakaran m" <prabha.gahon@gmail.com> wrote in message <jp9m5e$jbq$1@newscl01ah.mathworks.com>...
> if x >-1 & x<=0
> y=1+2*x
> else
> y=1-2*x
> end
- - - - - - - - -
  You are not using the 'if' condition properly. In Matlab when you apply the 'if' to a logical vector, as you have done here, what follows it is executed only if the vector is true for ALL elements of the vector. In your case the "y=1+2*x" would be executed only if x were between -1 and 0 for all 41 elements of x, which of course is not true. Therefore the "y = 1-2*x" is executed and applies to all elements of x.

  You should either use a for-loop with the 'if' in it with the inequalities to be applied to each element of x individually, as in "if x(i)>-1&x(i)<=0, etc.", or else write a vectorized version that works properly such as the following:

 y = 1 + 4*((x>-1)&(x<=0)).*x - 2*x;

or more tediously

 y = ((x>-1)&(x<=0)).*(1+2*x) + ((x<=-1)|(x>0)).*(1-2*x);

Notice that these have no 'if' in them.

Roger Stafford

Subject: if loop condition

From: Bruno Luong

Date: 20 May, 2012 06:18:42

Message: 3 of 5

As Roger explained, if does not behave on vector as you expect, because a single branching is made. You can use logical indexing:

y=1-2*x;
y(x >-1 & x<=0) = 1+2*x;

% Bruno

Subject: if loop condition

From: John D'Errico

Date: 20 May, 2012 07:13:31

Message: 4 of 5

"prabhakaran m" <prabha.gahon@gmail.com> wrote in message <jp9m5e$jbq$1@newscl01ah.mathworks.com>...
>
> i want to use this condition to find f(x)
> f(x) = 1 + 2x for x between -1 and 0 and f(x) = 1 - 2x for x between 0 and 1
>
> clear all
> N=41;
> x=linspace(-1,1,41)
> theta=-pi:2*pi/(N-1):pi;
>
> if x >-1 & x<=0
> y=1+2*x
> else
> y=1-2*x
> end
>
> i am getting the y values wrongly. what is the mistake i am doing. pls correct me

Well, the simple solution is as everyone is telling you.
But there are others.

y = 1 + ((x > 0)*2 - 1).*2*x;

Or perhaps this one is the simplest:

y = 1 - 2*abs(x);

I'll bet its the shortest.

John

Subject: if loop condition

From: prabhakaran m

Date: 20 May, 2012 08:21:05

Message: 5 of 5

Thank you all so much for your explanation and answers :)

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