The dry goods of MATLAB (1)

The most dry goods of MATLAB (1)-into the world of value

First of all, haha, welcome everyone to click in. Here, my brother will share a little bit of dry stuff about MATLAB. Getting started with the exam is not a problem. Don’t be more than, just do it--

A few more words...

Here, it is recommended that you have a basic knowledge of C language or other programming languages. You don’t need to be proficient, as long as there is something like that.
Besides, you have a certain understanding of linear algebra and matrices and are familiar with their basic knowledge.
Well, open the whole ...
First this section let us into the world value it

1. Generate matrix

1. Direct method

a = [1,2,3;4,5,6;7,8,9];

2. Colon one-dimensional matrix a = start: step size: end, step size 1 can be omitted

	b = 11:10;
	b = 1:10;    %与上一行代码等价``

3. The function generates
linspace (start, end, number of elements), arithmetic generates a one-dimensional matrix with the specified number of elements, and generates 100 if the number is omitted

c = linspace(0,10,5)

4. Special matrix

e = eye (4);  % eye(维数)的单位阵
z = zeros (1,4);  % zeros(维数)的全零阵
o = ones (4,1);   % ones(维数)的全1阵
r = rand(4);  % rand(维数)0-1随机分布的矩阵
rn = randn(4);  % randn(维数)0均值Gaussian随机分布的矩阵

2. Matrix operations

diag_a = diag(a,1);  % diag(行向量,主对角线上方第k条斜线)用行向量生成对角阵
tril_a = tril(a,1);  % tril(矩阵,主对角线上方第k条斜线)生成矩阵的下三角阵,triu上三角阵

1. +,-,*,/,^ operation
Take + as an example

a * a;  

Here is the knowledge of matrix multiplication, so I won’t explain more. The
implementation code is as follows
Insert picture description here

2. Point operation
% a. b, a./b, a.\b, a.^b corresponding element , /,,^ operation,
take .* as an example

a.*a

Here is the multiplication of the corresponding positions. The code is
Insert picture description here
here. Here is an idea. The dimensions of the matrices for point multiplication are equal.
3. Inverse matrix

pinv(a);   % 伪逆矩阵,当a不是方阵,求广义逆矩阵;当a是可逆方阵,结果与逆矩阵相同

4. Eigenvalues, eigenvectors

[v,D] = eig(a);   % 输出v为特征向量,D为特征值对角阵

5. Determinant

det(a);

6. Rank

rank(a);

7. Accompany

compan(b);

3. Modification of the matrix

1. Modification of the matrix
(1) Partial replacement

chg_a = a;
chg_a(2,3) = 4;  %(行,列)元素替换  这儿是把第2行第3列元素替换为4
chg_a(1,:) = [2,2,2];  %(行,:)替换行,为[ ]删除该行   这儿是把第一行替换成2,22,
chg_a(:,1) = [ ];    %(:,)替换列,为[ ] 删除该列     这儿是删除第一列

(2) Transpose

T_a = a' ; 

(3) Specified dimension splicing

c1_a = cat(1,a,a);   %垂直拼接
c2_a = cat(2,a,a);    %水平拼接 

(4) Variable dimension

rs_a = reshap(a,1,9);  % 元素个数不变,矩阵变为m*n

2. Information acquisition
(1) The number of rows and columns of the matrix

[row_a,col_a] = size(a);

(2) The largest in the ranks

len_a = length(a);

3. Creation of multi-dimensional array
(1) Direct method

mul_1(:,:,1) = [1,2,3;2,3,4];
mul_1(:,:,2) = [3,4,5;4,5,6];

(2) Expansion method

  mul_2 = [1,2,3;2,3,4];
  mul_2(:,:,2) = [3,4,5;4,5,6];

(3) cat method ( this method we usually use the most )

mul_31 = [1,2,3;2,3,4];
mul_32 = [3,4,5;4,5,6];
mul_3 = cat(3,mul_31,mul_32);  % 把a1a2按照“3”维连接

Four. String

1. Create

str0 = 'hello world';   %单引号引起
str1 = ‘I’'am' 'a' 'student';  %字符串中单引号写两遍
str2 = [‘I’'am' 'a' 'student'];  %方括号链接多字符串
str4 = strcat(str0, str1);  % strcat连接字符串函数
str5 = strvcat(str0, str1);  % strvcat连接产生多行字符串
str6 = double(str0);  % 取str0的ASCII值,也可用abs函数
str7 = char(str6);  % 把ASCII转为字符串

2. Operation
(1) Comparison

strcmp(str0,str1);   %相等为1,不等为0
strncmp(str0,str1,3);  %比较前三个是否相等
strcmpi(str0,str1);  % 忽略大小写比较
strncmpi(str0,str1,3);  %忽略大小写比较前三个是否相等

(2) Find and replace

strfind(str0,str1);  %在str0 找到str1 的位置
strmatch(str1,str0);  %在str0字符串数组中找到str1开头的行数
strtok(str0);   %截取str0第一个分隔符前的部分
strrep(str0,str1,str2);  %在str0 中用str2替换str1

(3) Other

upper(str0);   %转大写
lower(str0);   %转小写
strjust(str0,'right');  %将str0右对齐,left左对齐,center中间对齐
strtrim(str0);  % 删除str0开头结尾空格
eval(str0);  % 将str0作为代码执行

Five. Conversion

(1) % 2 -->

num2str,将数字转字符串;
dec2hex,将十进制转十六进制
str_b = num2str(b);   % abs,double取ASCII码;char把ASCII转字符串
abs_str = abs('aAaA');

Okay, shall we be here today?
I also learned a lot of knowledge when I turned it over. Students should study hard. If there are places that you don’t understand, copy the code and try to run it in matlab, I believe you will suddenly understand.
The knowledge learned today is often seen in daily learning, and it is also some basic content, and you will understand it after reading it a few times.
In the next section, I will bring you about the structure of matlab.
No longer than Bibi, my brother will leave first, see you next time. Goodbye

Guess you like

Origin blog.csdn.net/weixin_49005845/article/details/109626703