Asked by Rooy
on 14 Feb 2013

When p=0;

I have to use a loop to flip the value of p between 0 and 1 and each time it has to be different in a single line of code.

Thank you

*No products are associated with this question.*

Answer by Image Analyst
on 15 Feb 2013

Accepted answer

Is this what you're looking for?

p=0; for k = 1 : 5 % Here's the iterative loop it requested clc; % Here's the single line p = ~p pause(1); end fprintf('Done with demo!\n');

Youssef Khmou
on 15 Feb 2013

right !

Jan Simon
on 15 Feb 2013

Alternatively, if `p` should keep its type DOUBLE:

p = 1 - p;

Image Analyst
on 15 Feb 2013

That also works for *any* value or p in between 0 and 1, like I asked about in my comment way up at the top. So perhaps this is the *most* general solution.

Answer by Youssef Khmou
on 14 Feb 2013

Edited by Youssef Khmou
on 14 Feb 2013

your_vector=mod(0:1000,2); % single line code

for i=1:length(your_vector)

% YOUR CODE fprintf('%d\n',your_vector(i)) pause(0.3) end

Answer by Youssef Khmou
on 14 Feb 2013

for i=1:N if mod(i,2)==0 R(i)=1; end end

Rooy
on 14 Feb 2013

Would you count this a one line of code ? Thank you

Youssef Khmou
on 14 Feb 2013

ok look at the 2nd answer

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/63529#comment_129681

What does the "When p=0;" mean? You only have to flip when p=0???

When you say "flip the value of p between 0 and 1" does that mean that p is either 0 or 1 only, or does it mean p can have a value between 0 and 1, like p=.426973 and flipping means inverting like flipped_p = 1-p?

## Rooy (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/63529#comment_129813

This was the question given : Write a MATLAB program that will start with the variable p=0. After the initial assignment to p, have a loop flip the value of p between 0 and 1, changing it once each loop iteration USING A SINGLE LINE OF CODE. [HINT: This is a math challenge.] Display it each iteration, using the pause() function to stop the loop temporarily each iteration

Thank you everyone for all the help