MATLAB frequently used confusing operations must know and must know (1) matrix operations [in serial]

Logarithmic distinction

log()		%自然对数ln
log10()		%常用对数lg
log2()      %底为2对数lb
logN()      %底为N对数
logm()		%对矩阵求对数

Index distinction

exp()	%不能写成e^x
exp(1)	%不能写成e

Rounding function

round	%四舍五入取整
fix	    %朝0方向取整
floor	%朝-∞方向取整
ceil	%朝+∞方向取整

Remainder function


mod(-13,5)  %结果为3
rem(-13,5)  %结果为-2

plural

Since i and j can represent both iterative variables and imaginary parts, the iterative variables can be changed to ii and jj, which means that 1i is used to distinguish complex numbers

Array traversal

x(:)

x(j:end)

x(:,j)

If you know the number of elements required in the vector (not the spacing between each element), you can use the linspace function instead:

linspace(first,last,number_of_elements)

Note the use of commas (,) to separate the input of the linspace function

x = linspace(0,1,5)
x = 
    0    0.250    0.500    0.750    1.000

Random number matrix

x = rand(2)
x = 
    0.8147    0.1270
    0.9058    0.9134

The 2 in the rand(2) command specifies that the output will be a 2×2 uniformly distributed random number matrix

x = rand(2,3)
x = 
    0.6324    0.2785    0.9575
    0.0975    0.5469    0.9649
rand(m,n)            %生成m行n列的均匀分布的伪随机数
rand(m,n,‘double’)   %生成指定精度的均匀分布的伪随机数,参数还可以是’single’
rand(RandStream,m,n) %用指定的随机种子生成伪随机数

randn(m,n)            %生成m行n列的标准正态分布的伪随机数
randn(m,n,‘double’)   %生成指定精度的标准正态分布的伪随机数
randn(RandStream,m,n) %用指定的随机种子生成标准正态分布的伪随机数

randi(M)              %在(0,M)生成均匀分布的伪随机数
randi(M,m,n)          %在[0,M]生成m×n随机矩阵
x = randi([M,N],m,n)  %在[M,N]生成m×n随机矩阵

 

You can use the max function to determine the maximum value of a vector and its corresponding index value. The first output of the max function is the maximum value of the input vector. When executing a call with two outputs, the second output is the index value.

[xMax,idx] = max(x)

After R2009b, if index is not needed, it can be replaced with ~ignore output.

It may only need to contain the index of the maximum value in the vector:

density = data(:,2)
[~,ivMax] = max(v2)
densityMax = density(ivMax)

You can use a logical array as an array index. In this case, MATLAB will extract the array element whose index is true. The following example will extract all elements greater than 6 in v1.

v = v1(v1 > 6)
v =
    6.6678
    9.0698

Matrix transpose

B = A.' 	%转置
B = A'  	%共轭转置

Eigenvalue and eigenvector

E = eig(A)       %求矩阵A的全部特征值,构成向量E。
[X,D] = eig(A)   %求矩阵A的全部特征值,构成对角阵D并产生矩阵X,X各列是相应的特征向量。

Next preview: quick operation and program structure

Guess you like

Origin blog.csdn.net/weixin_43335260/article/details/108703403