I'm not sure why this loop is running infinitely; shouldn't it end as soon as nVal is equal to the value of inputM?:
clear;
clc;
inpuTm = input('Enter the value for m: ');
nVal = 2;
pofN = [0,1];
while nVal <= inpuTm
pNext = 2*(nVal-1)+(nVal-2);
pofN = [pofN,pNext];
end
disp(pofN);

답변 (1개)

dpb
dpb 2018년 11월 3일

0 개 추천

Yes, but neither nVal nor inpuTm are modified inside the while loop, so they remain unchanged and the loop either never executes or is an infinite loop depending upon the value of inpuTm.

댓글 수: 4

Delaney Andersen
Delaney Andersen 2018년 11월 4일
How do I fix that? If I change pNext to nVal, it should be modified and is still not working.
dpb
dpb 2018년 11월 4일
'Pends on what you're trying to accomplish...probably incrementing nVal in the loop I'd guess, but you don't indicate what you expect the result to be, specifically.
Delaney Andersen
Delaney Andersen 2018년 11월 4일
nVal should increase by 1 until it equals the 'inpuTm' to create a vector 'pofN' with nVal number of values.
Walter Roberson
Walter Roberson 2018년 11월 4일
Why not use a for loop?

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2018년 11월 3일

댓글:

2018년 11월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by