this should be easy I have a while loop that is infinite

조회 수: 4 (최근 30일)
SonOfAFather
SonOfAFather 2012년 9월 13일
When I run this it is inifinite and well that's not what i am looking for... i thought that by limiting the x value to 99 which is in the vector i could get it to stop but that's not the case. any ideas i know that it is going to be something simple...
clc, clear all, close all;
x = 1;
while x <= 99
x = 1:2:100
end
  댓글 수: 2
SonOfAFather
SonOfAFather 2012년 9월 13일
I see that i can use the break comand, but don't think that is the best choise here anyother options out there??
SonOfAFather
SonOfAFather 2012년 9월 13일
what do you guys think of this changing the while x<=99 to x==1? i found that the loop stops as i would want it to.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Fig
Matt Fig 2012년 9월 13일
In MATLAB, conditionals evaluate to true if ALL values of the conditional are non-zero or true. You are repeatedly creating a vector x inside the loop which has all of its values equal to or less than 99, so the conditional will evaluate to true every time. This is why you are getting an infinite loop.
Here is an example, using an IF statement. It evaluates the same way the WHILE loop does:
v = [1 2 -9]; % Notice ALL values are non-zero
if v, disp('IN IF 1'),end
v = [1 0 -9]; % Not all values are non-zero.
if v, disp('IN IF 2'),end
When you do a comparison (>,<,==, etc), you get a logical result. If you do a comparison on an array, you get a logical vector the same size as the array. This will go through a conditional expression the same as v did above: any false (0) values and the expression will not pass the statement.
v>-20 % This will pass [1 1 1]
v<0 % Will not pass, [0 0 1]
What are you trying to achieve with your loop?
  댓글 수: 5
Matt Fig
Matt Fig 2012년 9월 13일
편집: Matt Fig 2012년 9월 13일
I see. There are several ways to do that.
x = [];
n = 1;
while n<=100
x = [x n];
n = n + 2;
end
Another:
x = zeros(1,ceil(99/2)); % Not strictly necessary.
n = 1;
while n<=ceil(99/2)
x(n) = 2*n-1;
n = n + 1;
end
You could also be a smart alec:
T = true;
while T
x = 1:2:100;
T = false;
end
SonOfAFather
SonOfAFather 2012년 9월 13일
thanks matt i needed a laugh! :)

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 13일
편집: Azzi Abdelmalek 2012년 9월 13일
x = 1;
while x <= 99
x=x+2
% do
end
  댓글 수: 4
SonOfAFather
SonOfAFather 2012년 9월 13일
exactly it is a proble that we were given on a study guide i would love to just make it x = 1:2:100 , but the prof wants it to be a while loop.here is the question:
Complete the following problems:
a. Create a for loop that will output the numbers from 1 to 100 by 2s (1, 3, 5, …, 97, 99).
b. Rewrite the above for loop as a while loop.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 13일
x=1
while x<=100-2
x(end+1)=x(end)+2
end

댓글을 달려면 로그인하십시오.


Wayne King
Wayne King 2012년 9월 13일
편집: Wayne King 2012년 9월 13일
Why do you need a while loop here? If you just want a vector running from 1 to 99 in increments of 2, the following does the job.
x = 1:2:100;
With the previous statement you are creating a vector whose last element is 99 and since you don't increment that vector inside the while loop, you never exceed your terminating condition.
note the difference between that and
x = 1;
while x<= 99
x = x+1;
end
x
You see with the above, that when you exit the while loop, x is equal to 100, which terminates the loop. In your example, you never exceed 99 and that is why you have an infinite loop.
  댓글 수: 1
SonOfAFather
SonOfAFather 2012년 9월 13일
I need the while loop to because it is what the prof asked for. i agree that the
x = 1:2:100;
is much simpler and cleaner but that's what he wants.
But with 99 being greater-than-or-equal-to x shouldn't 99 be withing the vector since i can see it on the output?

댓글을 달려면 로그인하십시오.


Wayne King
Wayne King 2012년 9월 13일
If want you create a vector with a while loop (again not sure why you want to do that), there are many ways to code it.
clear x;
n = 1;
k = 1;
while k<100
x(n) = k;
n = n+1;
k = k+2;
end
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 13일
Is it a joke?
SonOfAFather
SonOfAFather 2012년 9월 13일
no its an actual problem. here is what i came up with a min ago:
clc, clear all, close all;
x = 0;
for x = 1:2:100;
disp(x)
end
display('while')
x1 = -1;
while x1 <=97;
x1=x1+2;
disp(x1)
end

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2012년 9월 13일
x = 1;
while x(end) < 99
x=[x x(end)+2];
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by