Fliping a value between zero and one
조회 수: 36 (최근 30일)
이전 댓글 표시
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
댓글 수: 2
Image Analyst
2013년 2월 15일
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?
채택된 답변
Image Analyst
2013년 2월 15일
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');
댓글 수: 3
Image Analyst
2013년 2월 15일
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.
추가 답변 (2개)
Youssef Khmou
2013년 2월 14일
편집: Youssef Khmou
2013년 2월 14일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!