1.柱状图(bar)
>>x = [55,63,69,70,75,78,82,84,85,88,90,96,100];
>>y = [1,2,1,6,4,7,2,1,3,2,4,2,1];
>>bar(x,y)
2.创建函数初部
计算一组加权数据的均值
functionave = myaverage(x, N)
sizex= size(x);
sizeN= size(N); 数组元素的个数
if sizex(2) ~= sizeN(2) 检查个数是否相等
disp('错误:数据必须具有相同的维数。')
else
total= sum(N);
s =x.*N;
ave =sum(s)/total;
3.for循环
for i= 1:num(2)
sumx= sumx + x(i);
end
4.装箱问题
f_abs= [2, 1, 0, 0, 3, 0, 0, 1, 0, 1, 0, 4, 0, 0, 2, 0, 1, 2, 0, 0, 3, 0, 1, 2, 0,0, 3];
binwidth= 1;
bins= [17:binwidth:43];
raw =[];
for i = 1:length(f_abs)
if f_abs(i) > 0
new =bins(i)*ones(1,f_abs(i));
else
new =[];
end
raw=[raw,new];
end
结果:
>>raw
raw =
Columns1 through 16
17 1718 21 21 21 24 26 28 28 28 28 31 31 33 34
Columns17 through 26
34 3737 37 39 40 40 43 43 43
>>ave = mean(raw) 均值
ave =
30.7308
>>md = median(raw) 中位数
md =
31
>>sigma = std(raw) 标准差
sigma=
8.3836
5.其他
① input
>>sqft = input('请输入房子的总平方数:')
请输入房子的总平方数:
输入:
1740.00
>>price = rate*sqft
price=
17400.00
>>disp('总价钱是$:'),disp(price)
总价钱是$:
17400.00
②switch语句
switch grade
case1
pay =40000
case( 2,3 )
pay =65000
case4
pay =85000
end