display函数
disp(['apple','banana','orange'])
applebananaorange
>> disp(['apple ','banana ','orange '])
apple banana orange
>> a = 'apple';b = 'banana'; c = 'orange';
>>display([a,b,c])
applebananaorange
>> display([a,' ',b,' ',c])
apple banana orange
num = input('请输入1-100的一个数:')
if num >= 1 && num <= 100
disp(num^2);
else
disp('wrong number');
end
while循环
t = 1;
while t <= 5
disp(t);
t = t+1;
end
1+…+100
i = 1;
sum = 0;
while i<=100
sum = sum + i;
i = i + 1;
end
disp(sum);
辗转相除法求最大公约数
a = input('a = ');
b = input('b = ');
r = mod(a,b);
while r ~= 0
a = b;
b = r;
r = mod(a,b);
end
display(b);
for循环
for i = 1: 5
disp(i)
end
# 1 2 3 4 5
for i = 5:-1:-1
disp(i)
end
#5 4 3 2 1 0 -1
for i = 5:2:10
disp(i)
end
#5 7 9
1+1/2+3/1+…+1/100
s = 0;
for i = 1:100
s = s + (1/i);
end
disp(s)
#5.1874
1-1/2+1/3-1/4…+1/100
s = 0;
for i = 1:100
if mod(i,2) == 1
s = s + (1/i);
else
s = s - (1/i);
end
end
disp(s)
#0.6882
遍历数组里的每个数
v = [1 2 3 4 5 6 7 8];
for i = v
disp(i);
end