一、求最大值
>> x=[1,2,3,4,5,6]
x =
1 2 3 4 5 6
>> temp=0
temp =
0
>> for k=1:1:6
if x(k)>temp
temp=x(k)
end
end
temp =
1
temp =
2
temp =
3
temp =
4
temp =
5
temp =
6
>> temp
temp =
6
二、求最小值
>> x=[1,2,3,4,5,6]
x =
1 2 3 4 5 6
>
本文介绍了如何在MATLAB中利用for和while循环求取数组的最大值和最小值。通过实例展示了循环在处理数组时的灵活性,包括求最大值的过程和求最小值的方法。
一、求最大值
>> x=[1,2,3,4,5,6]
x =
1 2 3 4 5 6
>> temp=0
temp =
0
>> for k=1:1:6
if x(k)>temp
temp=x(k)
end
end
temp =
1
temp =
2
temp =
3
temp =
4
temp =
5
temp =
6
>> temp
temp =
6
二、求最小值
>> x=[1,2,3,4,5,6]
x =
1 2 3 4 5 6
>
4732

被折叠的 条评论
为什么被折叠?