How can I change this loop to a switch loop?
2 次查看(过去 30 天)
显示 更早的评论
width=input('Enter a positive number: ');
disp(' ');
while width<0
width=input('Enter a positive number: ');
disp(' ');
end
%dimension input
dimension=input('Input an integer greater than 1: ');
disp(' ');
while (dimension<=1)
dimension=input('Input an integer greater than 1: ');
end
reorint=0;
while reorint==0
if isreal(dimension)&&mod(dimension,1)==0
reorint=1;
else
dimension=input('Input an integer greater than 1: ');
reorint=0;
end
end
%colors input
color1=input('Enter 1st color: red(r), yellow(y), green(g), black(k), blue(b), magenta(m), white(w), cyan(c): ','s');
disp(' ');
color2=input('Enter different 2nd color: red(r), yellow(y), green(g), black(k), blue(b), magenta(m), white(w), cyan(c): ','s');
disp(' ');
N=4;
R=width;
Phase=pi/4;
theta=zeros(1,N);
for b=1:N
theta(b)=2*pi*(b-1)*(1/4);
end
x1=R*cos(theta+Phase)/sqrt(2); %cosine function
y1=R*sin(theta+Phase)/sqrt(2); %sine function
for m= 0 : dimension-1
for n= 0 : dimension-1
if mod(m+n,2) == 0 %adding your columns and rows to fill them with 2 different colors
fill(x1+m*width, y1+n*width,color1);
else
fill(x1+m*width, y1+n*width,color2);
end
hold on;
end
hold on;
end
How can I change the for loops starting from where it say "for b=1:N;" and the rest of the code and make it into a switch loop? Even if I can't change all of it can a change a few parts of it?
2 个评论
Alexandra Harkai
2016-11-21
' switch ' and loop statements are different and not directly interchangeable the way you sound to want them.
Why do you want to change those loops to a switch statement?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!