首页 >> 大全

matlab入门一基本操作

2023-08-25 大全 37 作者:考证青年

//学习笔记,侵删,参考书西安电子科技大学出版社程序设计,学长传下来的书

一、矩阵的几种处理

1.生成矩阵与基本处理

中矩阵元素按列存储,可以仅用一个下标寻址,如下例中对于矩阵a,a(2)=4,a(6)=8;

若要对复数矩阵com做转置,com' 表示共轭转置,若要实现非共轭转置,用com.'

triu(),tril()分别是对矩阵(未必是方阵)进行取上三角,下三角矩阵。

clc;close all;
a=[1 2 3;4 5 6;7 8 9];%3*3 matrix
b=[6 2 4];
c = diag(a)%without ";" in the end means show the output on command window
d = diag(b)%diag()to extract the diagonal or generate diagonal matrix
e = diag(b,1)%generate 4*4 matrix,and the identity matrix in the upper right corner
f = diag(b,-1)%generate 4*4 matrix,and the identity matrix in the lower left corner
g = fliplr(a)%flip left and right
h = flipud(a)%flip up and downa11 = eye(5)%generate 5*5 identity matrix
a12 = eye(3,4)%generate 3*4 matrix with the identity matrix in the left
a13 = zeros(2,5)%generate 2*5 all zero matrix
a14 = ones(3,2)%generate 3*2 all one matrix%运行结果如下
c =159d =6     0     00     2     00     0     4e =0     6     0     00     0     2     00     0     0     40     0     0     0f =0     0     0     06     0     0     00     2     0     00     0     4     0g =3     2     16     5     49     8     7h =7     8     94     5     61     2     3a11 =1     0     0     0     00     1     0     0     00     0     1     0     00     0     0     1     00     0     0     0     1a12 =1     0     0     00     1     0     00     0     1     0a13 =0     0     0     0     00     0     0     0     0a14 =1     11     11     1

2.矩阵处理进阶

2.1矩阵扩大

clc;close all;
a=[1 2;3 4];
b=[a a+2;a-2 zeros(size(a))]%size(a)=[2 2],generate 4*4 matrix by matrix a
c=[a;5 10]%increase one row below a
d=[a [5;10]]%increase one column on the right of a
e=[[5;10] a]%increase one column on the left of aa1=[5 6;7 8];
f=cat(1,a,a1)%link by first dimension (to the row)
g=cat(2,a,a1)%link by second dimension
h=cat(3,a,a1)%link by third dimensioni=repmat(a,2,3)%generate 2*3 matrix which is formed by repeat matrix a%结果如下
b =1     2     3     43     4     5     6-1     0     0     01     2     0     0c =1     23     45    10d =1     2     53     4    10e =5     1     210     3     4f =1     23     45     67     8g =1     2     5     63     4     7     8h(:,:,1) =1     23     4h(:,:,2) =5     67     8i =1     2     1     2     1     23     4     3     4     3     41     2     1     2     1     23     4     3     4     3     4

2.2矩阵缩小

clc;close all;
a=[1:4;5:8;9:12;13:16]
%extraction
b=a(2:3,3:4)%to extract row:2 to 3,column:3 to 4
c=a([2 4],[1 3])%to extract row:2 and 4,column:1 and 3
%delete
a(2,:)=[]%to delete the second row
a(:,[1 3])=[]%to delete the first and the third column%运行结果如下
a =1     2     3     45     6     7     89    10    11    1213    14    15    16b =7     811    12c =5     713    15a =1     2     3     49    10    11    1213    14    15    16a =2     410    1214    16

预算会计操作入门_电脑技巧入门操作_

2.3逻辑函数

(a):如果a是一个向量,若其中所有元素都是非零,返回1,若有一个元素为零,返回0;如果a是一个矩阵,则返回一个行向量,用于检测每一列是否全为非零元素,如果某一列有一个值为零,则返回0,若某一列全为非零,才返回1.

fun2 any():规则与all类似,有非零返回1,全零才返回0.

2.4基本运算

左右除与矩阵的逆

inv(A):对矩阵A求逆,且A必须为方阵。如果A是非奇异方阵,则B/A = B*inv(A),A\B = inv(A)*B。/表示右除,\表示左除。

2.5特殊形式矩阵生成

线性间距向量

y=(a,b)可在a和b之间等间隔产生100个点,y=(a,b,n)则生成n个点。ps:含端点。

对数间距向量

y=(a,b,n)默认是50个,从

_电脑技巧入门操作_预算会计操作入门

。(a,pi)表示在

和pi之间生成这些点,这在DSP领域很有用。

二、随机数生成函数

clc;close all;
%Generate a 2*3 matrix in which the elements
%are uniformly distributed on [0,1]
a=rand(2,3)%Generate a 3*2 matrix in which the elements
%are uniformly distributed on [-5,5]
b=10*rand(3,2)-5%Generate a 3*5 matrix in which the elements
%follow a standard normal distribution
c=randn(3,5)%Generate a 2*3 matrix in which the elements 
%follow a normal distribution N(3,4)
d=2*randn(2,3)+3%generate 2*5 integer matrix in which the elements
%are uniformly distributed on [10,50]
e=randi([10 50],2,5)%generate a random complex number in which the real part
%and the imaginary part are uniformly distributed on (0,1)
f=rand+1i*rand%运行结果如下
a =0.0423    0.1892    0.58640.9730    0.6671    0.6751b =-1.3898   -4.80741.2028   -4.16133.1115    4.7480c =0.3998   -1.4969   -0.7258   -0.9427    1.8179-0.6548   -0.9048   -0.8665    1.3419   -0.3744-0.2963   -0.4042   -0.4218   -0.9884   -1.4517d =1.7626    5.1119    3.57484.8690    3.3205    4.2658e =12    11    39    45    1222    18    39    33    47f =0.8004 + 0.2859i

三、常用几个取整函数区别

fix向零方向取整,floor向负无穷方向取整,ceil向正无穷方向取整,round进行四舍五入。

四、取余函数

mod(X,Y) 返回值为X-Y.*floor(X./Y)

rem(X,Y) 返回值为X-Y.*fix(X./Y)

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了