How to convert from a for loop to a while loop

조회 수: 3 (최근 30일)
David Hughes
David Hughes 2015년 7월 7일
편집: Azzi Abdelmalek 2015년 7월 7일
function [y] = HW5_P2(n)
%This function file determines the value of n^2 over the range 1 to n
for i = 3:3:n
x = [1:i];
y = x.^2;
subplot(2,2,i/3);
plot(x,y),xlabel('x'),ylabel('y'),title(num2str(i/3))
end
end

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 7일
편집: Azzi Abdelmalek 2015년 7월 7일
Don't use the variable i, it's used by Matlab to represent complex numebers
ii=3
while ii<n
x = [1:ii];
y = x.^2;
subplot(2,2,ii/3);
plot(x,y),xlabel('x'),ylabel('y'),title(num2str(ii/3))
ii=ii+3
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