MatLab Study Notes: Getting Started - 2021.9.14~2021.9.18

Today is the first day, I plan to finish this tutorial in two days 

Interface knowledge

Command operation should be very familiar, it is similar to git or something

Editor: Click to create an editor to appear, the editor is mainly used for writing scripts

Notes 

%%and%

Command Window Common Commands

clc - clears all commands in the command window

clear all - clears all variables in the workspace

Matlab variable naming rules

1. Case sensitive

2. The length is limited, 63 bits should not be too long

3. Variables start with a letter and can contain letters, underscores and numbers and cannot use punctuation

Matlab data types

numbers, characters and strings, matrices, cell arrays , structures

1. Numbers: + - */

2. String with single quotes  ' ' 

        s= 'a'

        abs(s) %输出结果为数字

        char(97)  %97是阿斯克编码 输出结果为a

        num2str(65)  %把数字65转换为字符串‘65’

        length(str)  %字符串的长度

3. Matrix

Define the same row of the matrix with a space or comma to wrap the line with a semicolon

A=[1 2 3; 4 5 6; 7 8 9]   %定义一个矩阵

B=A'    %转置 行列对调

C=A(:)   %按列排列矩阵,将矩阵化为一列

D=inv(A)   %求逆  非方阵不能求逆

A*D   %矩阵相乘

E=zeros(10,5,3)   %10行5列3维 的全0矩阵

E(:,:,1)=rand(10,5)    %第一个维度  用10行5列的随机数填充
The difference between rand, randi, randn

rand(m,n)

rand(m,n,'double')

rand(RandStream,m,n)

1. Generate pseudo-random numbers of m rows and n columns The range of random numbers is 0~1

2. Generate uniform pseudo-random number parameters of specified precision with double and

single

3. Use the specified random seed to generate pseudo-random numbers

randi(iMax)

randi(iMax,m,n)

randi([iMin,Imax],m,n)

1. Uniformly distributed pseudorandom integers in the open interval 0~imax'2.

2. Generating Matrix

3. The open interval becomes imin to imax

randn syntax is the same as rand 1. Generate a pseudo-random number with a standard normal distribution with a mean of 0 and a variance of 1.

4. Cell array

A cell array is a type of array, and its internal elements can be of different data types, very similar to the structure in c

A=cell(1,6) % 生成一个一行六列的元胞数组
A{2}=eye(3)  %eye矩阵就是E  赋值给元胞数组的第二个 cell 注意matlab的数据是从1开始的,而非0
A{5}=magic(5) %生成一个五阶的幻方 赋值给元胞数组的第五个cell
B=A{5}
MAGIC
magic(n) 1. The magic method is used in matlab to generate an n-order magic square. The n-order magic square refers to an N*N matrix, and the values ​​of rows, columns or oblique additions are equal.

5. Structure

structure definition

books= struct('name',{
   
   {'machine learning','data minging'}},'price',[30,40])
books.name
books.name(1)
books.name{1}

Take a look at the running results

% Note that when books are defined, it is attribute name + attribute value, separated by commas

books = 

  A struct with the following fields:

     name: {'machine learning'  'data minging'}
    price: [30 40]

%books.name takes out the value of the property, which is a cell array
ans =   

  1×2 cell array

    {'machine learning'}    {'data minging'}

%books.name(1) is to take out the first cell of the cell array
ans =

  1×1 cell array

    {'machine learning'}

%books.name{1} takes a string, which is the value in the cell array
ans =

    'machine learning'

I feel it can be understood as &a, &a[1], a[1]

Matrix Construction and Four Operations

1. Definition and Construction of Matrix

A=[1 2 3 4 5 6 7 8]

>> A=[1 2 3 4 5 6 7 8]

A =

     1     2     3     4     5     6     7     8

B=1:2:9 % This is a sequence of starting values ​​from left to right, and the maximum step size does not exceed

>> B=1:2:9

B =

     1     3     5     7     9

C=repmat(B,3,2) % treat B as a whole and repeat three rows and two columns

>> C=repmat(B,3,2)

C =

     1     3     5     7     9     1     3     5     7     9
     1     3     5     7     9     1     3     5     7     9
     1     3     5     7     9     1     3     5     7     9

D=ones(2,4) % generates a two rows and four columns while the values ​​in the matrix are all 1

>> D=ones(2,4)

D =

     1     1     1     1
     1     1     1     1

2. Four operations on matrices

Matrix division:

(1) The right division formula A/B is equivalent to A*inv(B), that is, the inverse matrix of A right-multiplied by B;

(2) The left division formula A\B is equivalent to inv(A)*B, that is, the inverse matrix of A is left multiplied by B

X1=[7 5 3 ; 9 5 1]
X2=[4 5 6 ; 8 5 2]

X1 =

     7     5     3
     9     5     1


X2 =

     4     5     6
     8     5     2


X1+X2

years =

    11    10     9
    17    10     3


X1-X2

years =

     3     0    -3
     1     0    -1


X1*X2' % Note that the front row and back column should be the same

>> X1*X2'

years =

    71    87
    67    99


X1.*X2 % all plus. It means that the multiplication of the corresponding items is the number of the corresponding position of the front matrix and the back matrix.

>> X1.*X2

years =

    28    25    18
    72    25     2


X1./X2   

>> X1./X2

years =

    1.7500    1.0000    0.5000
    1.1250    1.0000    0.5000


X1/X2 % does not need to be transposed here

>> X1/X2

years =

    0.2500    0.7500
   -0.2500    1.2500

3. The subscript of the matrix

A=magic(5) % First generate a 5*5 magic square matrix

A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

A(2,3)

years =

     7
 

A(3,:) % colon means all, take out all
ans in the third line =

     4     6    13    20    22

[m,n]=find(A>20) The %find function means to find the coordinates in the matrix A that are greater than 20, because m is returned, and n is the form of a coordinate

m =

     2
     1
     5
     4
     3


n =

     1
     2
     3
     4
     5

MATLAB logic and flow control

1.if    else   end

2.for   end

3.while   end  

4.switch   case   end 

loop structure

for loop

for loop variable = initial value: step: end value

        execute statement 1

        .

        .

        execute statement n

end

The step size defaults to 1

For example, find the value of 1²+2²+...+5²

>>sum=0
>>for n=1:1:5
    sum=sum+n^2;
end

ps: for loops can be nested

>>sum=0

>>for i=1:5

        p=1

                for j=1:i;

                p=p*j;

        sum=sum+p

  end

while loop

In fact, it is similar to the language, while is followed by a conditional statement

>>s=0

>>n=1'

>>while n<=10

        s=s+n

        n=n+1

    end

branch structure

if....end structure

if condition is true

        execute conditional body

end exit the conditional body

>>a=100

>>b=20

>>if a>b

'established'

end

if..else..end statement body

>>a=100

>>b=20

>>if a>b

'established'

else

'invalid'

end

switch..case..end structure

switch expression (number or string)

        case numeric or string

                xxxx

        case numeric or string

        otherwise

                xxxx

end

But unlike language, there is no break

Guess you like

Origin blog.csdn.net/karonneveralone/article/details/120290694#comments_20323421