matlab入门一基本操作

//学习笔记 , 侵删 , 参考书西安电子科技大学出版社程序设计 , 学长传下来的书
一、矩阵的几种处理
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 matrixb=[6 2 4];c = diag(a)%without ";" in the end means show the output on command windowd = diag(b)%diag()to extract the diagonal or generate diagonal matrixe = diag(b,1)%generate 4*4 matrix,and the identity matrix in the upper right cornerf = diag(b,-1)%generate 4*4 matrix,and the identity matrix in the lower left cornerg = fliplr(a)%flip left and righth = flipud(a)%flip up and downa11 = eye(5)%generate 5*5 identity matrixa12 = eye(3,4)%generate 3*4 matrix with the identity matrix in the lefta13 = zeros(2,5)%generate 2*5 all zero matrixa14 = ones(3,2)%generate 3*2 all one matrix%运行结果如下c =159d =600020004e =0600002000040000f =0000600002000040g =321654987h =789456123a11 =1000001000001000001000001a12 =100001000010a13 =0000000000a14 =111111
【matlab入门一基本操作】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 ac=[a;5 10]%increase one row below ad=[a [5;10]]%increase one column on the right of ae=[[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 dimensionh=cat(3,a,a1)%link by third dimensioni=repmat(a,2,3)%generate 2*3 matrix which is formed by repeat matrix a%结果如下b =12343456-10001200c =1234510d =1253410e =5121034f =12345678g =12563478h(:,:,1) =1234h(:,:,2) =5678i =121212343434121212343434
2.2矩阵缩小
clc;close all;a=[1:4;5:8;9:12;13:16]%extractionb=a(2:3,3:4)%to extract row:2 to 3,column:3 to 4c=a([2 4],[1 3])%to extract row:2 and 4,column:1 and 3%deletea(2,:)=[]%to delete the second rowa(:,[1 3])=[]%to delete the first and the third column%运行结果如下a =12345678910111213141516b =781112c =571315a =1234910111213141516a =2410121416

matlab入门一基本操作

文章插图
2.3逻辑函数
(a):如果a是一个向量 , 若其中所有元素都是非零 , 返回1 , 若有一个元素为零 , 返回0;如果a是一个矩阵 , 则返回一个行向量 , 用于检测每一列是否全为非零元素 , 如果某一列有一个值为零 , 则返回0 , 若某一列全为非零 , 才返回1.
fun2any():规则与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个 , 从
matlab入门一基本操作

文章插图

。(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 distributionc=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.04230.18920.58640.97300.66710.6751b =-1.3898-4.80741.2028-4.16133.11154.7480c =0.3998-1.4969-0.7258-0.94271.8179-0.6548-0.9048-0.86651.3419-0.3744-0.2963-0.4042-0.4218-0.9884-1.4517d =1.76265.11193.57484.86903.32054.2658e =12113945122218393347f =0.8004 + 0.2859i