第二讲、矩阵
- 这节课的内容是矩阵相关知识,我认为,矩阵是matlab一切操作的基础。
- 本节课内容所在位置:
一、矩阵的创建
- 基本创建方式
% 行向量创建
a = [1,2,3,4]
a =
1 2 3 4
% 列向量创建
b = [1;2;3;4]
b =
1
2
3
4
% 矩阵创建
c = [1,2,3;4,5,6]
c =
1 2 3
4 5 6
- 冒号创建
% 行向量创建
a = 1:4
% 列向量创建
a = [1:4]'
% 矩阵创建
a = [1:3;4:6]
a =
1 2 3 4
a =
1
2
3
4
a =
1 2 3
4 5 6
- 拼接创建
A = [1,2;3,4]
B = [2,2;2,2]
C = [1,1,1,1]
D=[A,B;C]
n = (0:9)';
pows = [n n.^2 2.^n]
A =
1 2
3 4
B =
2 2
2 2
C =
1 1 1 1
D =
1 2 2 2
3 4 2 2
1 1 1 1
pows =
0 0 1
1 1 2
2 4 4
3 9 8
4 16 16
5 25 32
6 36 64
7 49 128
8 64 256
9 81 512
- 句柄函数创建
f1 = @(x)x.^2+1;
f2 = @(x)10.^x;
a = f1(1:5)
b = f2(1:5)
% 当然,我们不用句柄函数,也能创建各种各样的矩阵
a = [1:5].*[1:5]+1
a =
2 5 10 17 26
b =
10 100 1000 10000 100000
a =
2 5 10 17 26
- 特殊矩阵
a = linspace(0,1,5)
b = logspace(0,5,6)
[c,d] = meshgrid(1:3,2:4)
eye(3)
ones(2,3)
zeros(2,3)
rand(4,4)
a =
0 0.2500 0.5000 0.7500 1.0000
b =
1 10 100 1000 10000 100000
c =
1 2 3
1 2 3
1 2 3
d =
2 2 2
3 3 3
4 4 4
ans =
1 0 0
0 1 0
0 0 1
ans =
1 1 1
1 1 1
ans =
0 0 0
0 0 0
ans =
0.6353 0.0860 -0.3210 -1.2316
-0.6014 -2.0046 1.2366 1.0556
0.5512 -0.4931 -0.6313 -0.1132
-1.0998 0.4620 -2.3252 0.3792
二、索引
- 基本索引
A = magic(3)
A(8)
A(3,2)
A =
8 1 6
3 5 7
4 9 2
ans =
7
ans =
9
- 冒号索引
A = magic(3)
A(1:3,1);
A(1,1:3)
A(1:2,:)
A(:) % 返回值变成了一维列向量
A =
8 1 6
3 5 7
4 9 2
ans =
8 1 6
ans =
8 1 6
3 5 7
ans =
8
3
4
1
5
9
6
7
2
- 括号矩阵索引
A = magic(3)
A([1,3],[1,3])
A([1,3,5])
A([1,3;1,3])
A =
8 1 6
3 5 7
4 9 2
ans =
8 6
4 2
ans =
8 4 5
ans =
8 4
8 4
- 逻辑索引
x = [2.1 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8];
x = x(isfinite(x))
x =
2.1 1.7 1.6 1.5 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8
三、矩阵基本运算
- 加减乘除
% +
% Scalar-array addition
% A+b
% [6,3]+2=[8,5]
% -
% Scalar-array subtraction
% A-b
% [8,3]-5=[3,-2]
% +
% Array addition
% A+B
% [6,5]+[4,8]=[10,13]
% -
% Array subtraction
% A-B
% [6,5]-[4,8]=[2,-3]
% *
% Matrix multiplication
% A*B
% [3,5]*[4,8]'=52
% .*
% Array multiplication
% A.*B
% [3,5].*[4,8]=[12,40]
% ./
% Array right division
% A./B
% [2,5]./[4,8]=[2/4,5/8]
% .\
% Array left division
% A.\B
% [2,5].\[4,8]=[4/2,8/5]
% .^
% Array exponentiation
% A.^B
% [3,5].^[2,4]=[3^2,5^4]
- 关系运算
% eq == Determine equality
% ge >= Determine greater than or equal to
% gt > Determine greater than
% le <= Determine less than or equal to
% lt <= Determine less than
% ne ~= Determine inequality
% isequal Determine array equality
% isequaln Determine array equality, treating NaN values as equal
- 逻辑运算
% all Determine if all array elements are nonzero or true
% any Determine if any array elements are nonzero
% false Logical 0 (false)
% find Find indices and values of nonzero elements
% islogical Determine if input is logical array
% logical Convert numeric values to logicals
% true Logical 1 (true)
四、常用方法
- arrayfun:[B1,B2,…,Bm]=arrayfun(func,A1,A2,…,An,name,value)
S(1).f1 = rand(1,5);
S(2).f1 = rand(1,10);
S(3).f1 = rand(1,15);
A = arrayfun(@(x) mean(x.f1),S)
B = arrayfun(@(x) mean(x.f1),S,'UniformOutput',false) % uniformoutput false的应用 返回的是元胞数组
A =
0.6164 0.5122 0.4424
B =
1x3 cell 数组
{[0.6164]} {[0.5122]} {[0.4424]}
S(1).X = 5:5:100; S(1).Y = rand(1,20);
S(2).X = 10:10:100; S(2).Y = rand(1,10);
S(3).X = 20:20:100; S(3).Y = rand(1,5);
figure
hold on
p = arrayfun(@(a) plot(a.X,a.Y),S);
p(1).Marker = 'o';
p(2).Marker = '+';
p(3).Marker = 's';
hold off
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FAeAkCw8-1593938575424)(output_31_0.png)]
- 对自身的拼接循环
A=[1,2,3;4,5,6;7,8,9];
[l,c]=size(A);
b=A(:,1);
for i=2:c
b=[b A(:,1:i)];
end
b
[l,c]=size(A);
coeff=logspace(0,c,c+1);
b=A(:,1);
for i=2:c
b=[b [A(:,1:i)]*coeff(i:-1:1)'];
end
b
b =
1 1 2 1 2 3
4 4 5 4 5 6
7 7 8 7 8 9
b =
1 12 123
4 45 456
7 78 789
- find函数
A = reshape([1:9],3,3)'
ind=find(A>3) % 注意,其返回的是列向量
[m,n]=find(A>3)
A =
1 2 3
4 5 6
7 8 9
ind =
2
3
5
6
8
9
m =
2
3
2
3
2
3
n =
1
1
2
2
3
3
- ind2sub&sub2ind
ind = find(A>3)
[m n] = find(A>3)
IND = sub2ind(size(A),m,n)
[I J] = ind2sub(size(A),ind)
ind =
2
3
5
6
8
9
m =
2
3
2
3
2
3
n =
1
1
2
2
3
3
IND =
2
3
5
6
8
9
I =
2
3
2
3
2
3
J =
1
1
2
2
3
3
- 元素改变和删除
A=[1 2 3 4;5 6 7 8;9,10,11,12]
%删除A的第1行的数据
A(1,:)=[]
%删除A的第1列的数据
A(:,1)=[]
% 删除某个数字,删除某一个数字后,返回的是一个一维数列
A(sub2ind(size(A),1,1))=[]
A =
1 2 3 4
5 6 7 8
9 10 11 12
A =
5 6 7 8
9 10 11 12
A =
6 7 8
10 11 12
A =
10 7 11 8 12
- 查询矩阵大小
%由rand命令生成的一个4×3×2的三维矩阵
A=rand(4,3,2);
%得到矩阵的大小信息
num=size(A)
num=length(A)
num=size(A,1)
num=size(A,2)
num=size(A,3)
num =
4 3 2
num =
4
num =
4
num =
3
num =
2
- max,sum,mean
a=[2 3;3 6;4 9]
b=[1 4;4 5;5 8]
max(a)
max(a,[],1)
max(a,[],2)
max(a')
max(a,b)
a =
2 3
3 6
4 9
b =
1 4
4 5
5 8
ans =
4 9
ans =
4 9
ans =
3
6
9
ans =
3 6 9
ans =
2 4
4 6
5 9
- sort
A = magic(3)
sort(A) %注意比较
sortrows(A) %注意比较
sortrows(A,2) %注意比较,基于第二行的大小进行排序
A =
8 1 6
3 5 7
4 9 2
ans =
3 1 2
4 5 6
8 9 7
ans =
3 5 7
4 9 2
8 1 6
ans =
8 1 6
3 5 7
4 9 2
- rehape
a=[1,2,3;4,5,6]
a=reshape(a,3,2)% 注意看他是怎么reshape的,他是按照索引先拍成一行,然后reshape,
% 且reshape的第二个参数是行的个数,第三个是列的个数。
a =
1 2 3
4 5 6
a =
1 5
4 3
2 6
- 遍历
% 一维矩阵遍历
a = 1:5;
% 方式一、
for i=a
i
end
% 方式二
for i=1:length(a)
i
end
i =
1
i =
2
i =
3
i =
4
i =
5
i =
1
i =
2
i =
3
i =
4
i =
5
% 二维矩阵遍历
a = [1,2,3;4,5,6];
% 方式一
for i = a
i
end
% 方式二
for i = 1:size(a,1)
for j = 1:size(a,2)
a(i,j)
end
end
% 方式三
[I,J] = meshgrid(1:size(a,1),1:size(a,2))
I.^2+3.*J
i =
1
4
i =
2
5
i =
3
6
ans =
1
ans =
2
ans =
3
ans =
4
ans =
5
ans =
6
I =
1 2
1 2
1 2
J =
1 1
2 2
3 3
ans =
4 7
7 10
10 13