1.矩阵乘法
a=[1 2 3 4]
b=[1;2;3;4]
内积乘
a*b=1*1+2*2+3*+4*4
>> a=[1 2 3 4];
>> b=[1;2;3;4];
>> a*b
ans =
30
外积乘
b*a
>> a=[1 2 3 4];
>> b=[1;2;3;4];
>> b*a
ans =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
2.Array Index
>> A=[1 21 6;5 61 7; 9 12 15]
A =
1 21 6
5 61 7
9 12 15
>> A(2) %说明 索引是从上到下数的
ans =
5
>> A(1,2) % 1表示 row 2表示col
ans =
21
>> A([ 1 3 5]) %[] 表示多个索引的数
ans =
1 9 61
>> A([1 3;1 3])
ans =
1 9
1 9
>> A([1 3],[1 3]) %前面的表示 row 1 3 后面的表示col 1 3 两者的交集
ans =
1 6
9 15
>>
3.Colon Opreator
注意:多维数组的长度要一致
>> A=[1:2:5;1:2:6;2:1:9]
要串联的数组的维度不一致。
>> A=[1:5;2:3:14;-2:0.5:0]
A =
1.0000 2.0000 3.0000 4.0000 5.0000
2.0000 5.0000 8.0000 11.0000 14.0000
-2.0000 -1.5000 -1.0000 -0.5000 0
>> A(3,:)=[]
A =
1 2 3 4 5
2 5 8 11 14
>>
4.Array Manipulation
>> A=[ 1 2 3; 4 5 4; 9 8 7];
>> B=[3 3 3; 2 4 9 ; 1 3 1];
>> a=2;
>> x1=A+a
x1 =
3 4 5
6 7 6
11 10 9
>> x2=A/a
x2 =
0.5000 1.0000 1.5000
2.0000 2.5000 2.0000
4.5000 4.0000 3.5000
>> x3=A./a
x3 =
0.5000 1.0000 1.5000
2.0000 2.5000 2.0000
4.5000 4.0000 3.5000
>> x4=A^a
x4 =
36 36 32
60 65 60
104 114 108
>> x5=A.^a
x5 =
1 4 9
16 25 16
81 64 49
>> C=A'
C =
1 4 9
2 5 8
3 4 7
>> y1=A+B
y1 =
4 5 6
6 9 13
10 11 8
>> y2=A*B %10=1*3+2*2+3*2 20=1*3+2*4+3*3
y2 =
10 20 24
26 44 61
50 80 106
>> y3=A.*B % 3=1*3 2*3 col * row
y3 =
3 6 9
8 20 36
9 24 7
>> y4=A/B
y4 =
0.0714 0.2857 0.2143
1.1667 0 0.5000
3.2619 -0.2857 -0.2143
>> y5=A./B
y5 =
0.3333 0.6667 1.0000
2.0000 1.2500 0.4444
9.0000 2.6667 7.0000
>>
5.% 注释
%%表示一个区块
Ctrl+I自动缩放
6. prod(1:n) n!
1e100 =10^100
7.值重复使用的问题
for n=1:10
a(n)=2^n;
end
disp(a)
%%
for n=1:2:10
a(n)=2^n;
end
disp(a)
两次a的输出结果是一样的,因为a已经赋值了并且存储了,所以在使用a的时候,咋命令窗口输出clear a。
8.预宣告会加快运行速度
A=zeros(200,200) 比A(ii,jj)节省时间。 因为A(ii,jj)的大小一直在变化。
9.Crtl+C停止当前进度
10.function handle
power = @(x, n) x.^n
@(x, n) 表示传入参数
x.^n 表示执行的方法
11.字符倒叙
>> s1='dklckkjckf';
>> s1(end:-1:1)
ans =
'fkcjkkclkd'
>> fliplr(s1)
ans =
'fkcjkkclkd'
>>
12.结构
>> studen.name="123";
>> studen.id="1";
>> studen.number=123412;
>> studen.grade=[100,75,73;95,91,85;100 98 72];
>> studen
studen =
包含以下字段的 struct:
name: "123"
id: "1"
number: 123412
grade: [3×3 double]
>> studen.grade
ans =
100 75 73
95 91 85
100 98 72
>> studen.grade(3)
ans =
100
%继续添加数据的方式
>> studen(2).name="123";
13.Cell Array
>> A(1,1)={[1 2 3; 4 5 6]};
>> A(1,2)={'NI HAO'};
>> A
A =
1×2 cell 数组
{2×3 double} {'NI HAO'}
>>
>> A{1,1}=[1 2 3; 4 5 6];
>> A{1,2}=['NI HAO'];
>> A
A =
1×2 cell 数组
{2×3 double} {'NI HAO'}
>>
查看数据
>> A(1,1)
ans =
1×1 cell 数组
{2×3 double}
>> A{1,1}
ans =
1 2 3
4 5 6
>> A{1,1}(1)
ans =
1
>>
14 多维度数组 Multidimensional Array
A{1,2,3}=[1 2 3] 第一个row 第二个 col 第三个layer
15.reshape
>> A={'jAMES bond',{1 2 ; 3 4 ;5 6};pi,magic(5)};
>> A
A =
2×2 cell 数组
{'jAMES bond'} {3×2 cell }
{[ 3.1416]} {5×5 double}
>> C=reshape(A,1,4)
C =
1×4 cell 数组
{'jAMES bond'} {[3.1416]} {3×2 cell} {5×5 double}
>>
16.File Access
exl读写
score =xlsread('04Score.xlsx')
score =xlsread('04Score.xlsx')
>> M=mean(score')';
%将数据写入
>> xlswrite('04Score.xlsx',M,1,'E2:E4')
%将标题写入
>> xlswrite('04Score.xlsx',{'Mean'},1,'E1');
>> [Score Header] =xlsread('04Score.xlsx')
>> x=0:pi/10:pi;y=sin(x);fid=fopen('sinx.txt','w');
>> for i=1:11
fprintf(fid,'%5.3f %8.4f\n',x(i),y(i));
end
>> fclose(fid);
>> type sinx.txt
0.000 0.0000
0.314 0.3090
0.628 0.5878
0.942 0.8090
1.257 0.9511
1.571 1.0000
1.885 0.9511
2.199 0.8090
2.513 0.5878
2.827 0.3090
3.142 0.0000
>>
fid =fopen('snix.txt','r');i=1;
while ~feof(fid)
no1(i)=fscanf(fid,'%g',1);
no2(i)=fscanf(fid,'%g\n');
i=i+1;
end
fclose(fid);
18.绘图
一张图同时绘制多个曲线 用hold on
>> hold on
>> plot(cos(0:pi/20:2*p