Zerobased entry to Matlab (an introductory blog that can be learned in two hours)
Table of Contents
 Introduction to matlab with zero foundation

 Preface
 1. Interface understanding
 2. Variable naming
 3. Data types
 4. Cell arrays and structures
 5. Matrix operations
 6. Program structure
 7. Basic drawing operations

 7.1. Twodimensional plane drawing
 7.2. Threedimensional drawing
 8. Save and export graphics
 9. Supplement
Introduction to matlab with zero foundation
Foreword
This article is very suitable for the introductory learning of MATLAB, and this is also the note I learned when I was getting started.
Although it is said to be a “zerobased” introduction to matlab, it will naturally be easier to learn if you have other programming language foundations.
Special thanks to: UP Master of Station B—Xiao Anan who loves research
1. Interface understanding
2. Variable naming
Note: Notes in Matlab
%% Comments on a single line (with upper and lower horizontal lines)
% normal comments
1) Clear environment variables and commands
clear all Clear all variables in Workspace (right workspace)
clc Clear all commands in the Command Window (command line window)
2) Variable naming rules
①Variable names are casesensitive
②The length of the variable name does not exceed 63 characters (hhh, no one will make the variable name so long~)
③The variable name is concise and clear, try to be as clear as possible
3. Data type
1) Numbers
2 + 4
107
3*5
8/2
2) Characters and strings
s = ‘a’ (single quotes indicate a string)
==abs(s)==ASCII
char(97) output a (ASCII code to string)
num2str(65) output the number 65
str=’I love MATLAB & Machine Learning’
length(str) string length
doc num2str
3) Matrix (the most NB thing in Matlab)
A = [1 2 3; 4 5 2; 3 2 7]
B = A'
Transpose A, change row to column，Column to row
C = A(:)
vertical pull Long (not easy to describe, just look at the picture below)
D = inv(A)
Inverse matrix (only a square matrix can be used to find the inverse matrix)
Execute the following two statements
D = inv(A) (inverse matrix)
A * D (equivalent to the inverse of A×A)
E = zeros(10,5,3)
Create a 10row, 5column, 3dimensional matrix of all zeros
E(:,:,1) = rand(10,5)
rand generates uniformly distributed pseudorandom numbers. Distributed between (0~1)
Main syntax: rand(m,n) generates uniformly distributed pseudorandom numbers with m rows and n columns
rand(m,n,’double’) generates uniformly distributed pseudorandom numbers with specified precision, and the parameter can also be ‘single’
rand(RandStream,m,n) uses the specified RandStream (random seed) to generate pseudorandom numbers
E(:,:,2) = randi(5,10,5)
randi generates uniformly distributed pseudorandom numbers
Main syntax: randi(iMax) generates uniformly distributed pseudorandom numbers in the open interval (0, iMax)
randi(iMax,m,n) generates mXn type random matrix in the open interval (0,iMax)
r = randi([iMin,iMax],m,n) generates mXn type random matrix in the open interval (iMin, iMax)
E(:,:,3) = randn(10,5)
randn generates a standard normal distribution of pseudorandom numbers (mean 0, variance 1)
Main syntax: Same as above
4. Cell arrays and structures
Cell array: It is a unique data type in MATLAB, and it is a kind of array. Its internal elements can belong to different layout types. In terms of conceptual understanding, it can be regarded as similar to C The structure in the language is very similar to the object in C++. Cell array is a characteristic data type in MATLAB, which is different from other data types (such as character type, character array or string, and general arithmetic data and arrays). Its unique access data method determines itsIt has the characteristics of querying information, which can be traced gradually until all variables are translated into basic data information. Its class function output is cell (cell)
% cell array
A = cell(1,6)
A{2} = eye(3) Matlab subscripts before version 21 start from 1
A{5} = magic(5)
B = A{5}
Note: magic: Literally means Rubik’s cube, the meaning of magic. Used in MATLAB to generate magic squares of order n. For example, the thirdorder magic square is nine numbers from 1 to 9, which form a 3*3 matrix, so that the sum of the three numbers in the three directions of the matrix is always the same regardless of whether it is horizontal, vertical or oblique. Magic square is a very old problem, just try it out!
Structure
% structure
books = struct('name',{{'Machine Learning','Data Mining'}},'price',[30,40])
books.name % attribute
books.name(1)
books.name{1}
5. Matrix operation
1) Definition and construction of matrix
A = [1,2,3,4,5,6,5,4,6]
B = 1:2:9 % The second parameter is the step size, which cannot be defaulted
B = 1:3:9
C = repmat(B,3,2) % repeat 3 rows and 2 columns
D = ones(2,4) % Generate a matrix of all 1s with 2 rows and 4 columns
2) Four operations of matrix
A = [1 2 3 4; 5 6 7 8]
B = [1 1 2 2; 2 2 1 1]
C = A + B
D = A  B
E = A * B'
F = A .* B % .* indicates that the corresponding items are multiplied
G = A / B % is equivalent to the inverse of A*B G*B = A G*B*pinv(B) = A*pinv(B) G = A*pinv(B), equivalent to A times B
H = A ./ B % ./ means division of corresponding items
3) The subscript of the matrix
A = magic(5)
B = A(2,3)
C = A(3,:) %: to fetch all, then this statement means to fetch the third row
D = A(:,4) % take the fourth column
[m,n] =find(A > 20) % find the serial number value/matrix greater than 20
% takes the index value
6. Program structure
7. Basic drawing operations
7.1. Twodimensional plane drawing
%1. Twodimensional plane drawing
x = 0:0.01:2*pi % defines the range of x, the second parameter indicates the step size
y = sin(x)
figure % build a curtain
plot(x,y) % draw the current twodimensional plan
title('y = sin(x)') % title
xlabel('x') %x axis
ylabel('sin(x)') %y axisxlim([0 2*pi]) % range of x coordinate value
x = 0:0.01:20;
y1 = 200*exp(0.05*x).*sin(x);
y2 = 0.8*exp(0.5*x).*sin(10*x);
figure
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot'); % share a coordinate system of x, and have different values in y
% set the corresponding label
set(get(AX(1),'Ylabel'),'String','Slow Decay')
set(get(AX(2),'Ylabel'),'String','Fast Decay')
xlabel('Time(\musec)')
title('Multiple Decay Rates')
set(H1,'LineStyle','')
set(H2,'LineStyle',':')
7.2. Threedimensional drawing
%2. Threedimensional drawing
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
xlabel('sin(t)')
ylabel('cos(t)')
zlabel('t')
%hold on
%hold off %do not keep the current operation
grid on % Draw the picture and add some grid lines to the picture
axis square % makes the whole graph (along with the coordinate system) cube
Note: About the usage of hold on and hold off:Click here
8. Save and export graphics
If you directly capture the image generated by matlab by screenshot, it will affect the clarity of the image. Therefore, we suggest that you can use the following methods to save and export graphics.
1) As shown in the picture
2) Edit→Copy option
The corresponding element can be adjusted
3) Edit → Figure Properties
4) File→Export Settings
By adjusting pixel value attributes such as width and height, the text can still be clear even if the picture is small.
This is the end of the basic part of Matlab, let’s make a little addition~
9. Supplement
[x,y,z] = peaks(30); The %peaks command is used to generate bimodal functions or plot bimodal functions
mesh(x,y,z)
grid
End~
Thank you for your support, likes, favorites, Pay attention and criticize and correct~