当前位置:网站首页>Summary of MATLAB basic grammar
Summary of MATLAB basic grammar
2022-07-25 00:37:00 【Brave NN】
1. Interface understanding
- Command line input clc: Clear the command line window
- Command line input clear all: Clear the right workspace
- %: Comment code
2. Variable naming
- Case sensitive
- Start with a letter , You can use underscores
3. data type
- Numbers :abs()
- Characters and strings : String in single quotes 、char()、length()
- matrix
A=[1 2 3; 4 5 6; 7 8 9]
B=A' % Transposition
C=A(:) % Vertical stretching
D=inv(A) % Seeking inverse
A*D
E=zeros(10,5,3) % 10 That's ok 5 Column 3 dimension
E1=rand(10,5) % 0-1 A pseudo-random number uniformly distributed between
E2=randi([3,7],10,5) % A fixed range of random integers
E3=randn(10,5) % Pseudorandom number of standard normal distribution ( The mean for 0, The variance of 1)
- Cell array
A=cell(1,6) % 1 That's ok 6 A cellular array of columns
A{
2}=eye(3) % 3 That's ok 3 Unit matrix of columns
A{
5}=magic(4) % 4 Step magic square
- Structure
books=struct('name',{
{
'Machine Learning','Data Mining'}},'price',[30,40])
books.name
books.name(1) % 1×1cell
books.name{
1} % character string
4. Matrix construction and four operations
- Matrix construction
A=[1 2 3 4 5 6]
B=1:2:9 % The scope is 1-9, In steps of 2
C=repmat(B,3,2) % Give Way B Matrix repetition 3×2 Time
D=ones(4,4) % Generate 1 individual 4×4 Of all the 1 matrix
application :
Solve a system of linear equations :
A=[5 6 -12; 6 5 -2; 4 -9 2;]
B=[1 129 17]'
invA=inv(A)
X=invA*B
- arithmetic
A=[1 2 3 4; 5 6 7 8]
B=[4 3 2 1; 8 7 6 5]
C=A+B
D=A-B
E=A*B' % matrix multiplication ,A The line of =B The column of
F=A.*B % Multiply the corresponding terms
G=A/B % G*B=A
H=A./B % The corresponding term is divided by
- The subscript of a matrix
A=magic(5)
B=A(2,3) % take A Matrix No 2 Xing di 3 Number of columns
C=A(3,:) % take A The order of the matrix 3 That's ok
D=A(:,4) % take A The order of the matrix 4 Column
[m,n]=find(A>15) % find A Greater than in matrix 15 Number of numbers , And expressed by subscript
5. Program structure
- for
% seek 1-5 The sum of the squares
sum=0;
for i=1:5
sum=sum+i^2;
end
- if
a=100;
b=20;
if a>b
' establish '
else
' Don't set up '
end
- while
% seek 1-10 And
sum=0;
n=1;
while n<=10
sum=sum+n;
n=n+1;
end
6. 2D plane drawing
Example 1:
x=0:0.01:2*pi;
y=sin(x);
figure % Curtain
plot(x,y)
title('y=sin(x)')
xlabel('x')
ylabel('sin(x)')
xlim([0 2*pi])

Example 2:
x=linspace(0,30,500); % 0-30 Between 500 Elements
y=x.*sin(x); % Gain sine function
plot(x,y,'LineWidth',1.5); % The line width is 1.5
title(' Gain sine function ');
xlabel('\alpha');
ylabel('sin(\alpha)');
legend('sin(\alpha)'); % Insert line callout
text(2,2*sin(2),'\rightarrow Okey!'); % Insert text and arrows
grid on; % Show gridlines
box on; % Show axis box

Example 3:
x1=linspace(0,2*pi,100);
x2=linspace(0,3,100);
y1=1/3*x1.*sin(2*x1);
y2=x2.^2+2*x2;
plot(x1,y1,'r:',x2,y2,'b--');

Example 4:
x=linspace(0,2*pi,60);
subplot(2,2,1);
plot(x,sin(x)-1);
title('sin(x)-1');
axis([0,2*pi,-2,0]);
subplot(2,1,2);
plot(x,cos(x)+1);
title('cos(x)+1');
axis([0,2*pi,0,2]);
subplot(4,4,3);
plot(x,tan(x));
title('tan(x)');
axis([0,2*pi,-40,40]);
subplot(4,4,8);
plot(x,cot(x));
title('cot(x)');
axis([0,2*pi,-35,35]);

7. Three dimensional drawing
Example 1:
x=0:pi/50:10*pi;
plot3(sin(x),cos(x),x);
xlabel('sin(x)');
ylabel('cos(x)');
zlabel('t');
grid on;
axis square; % Set the current coordinate system drawing to square

Example 2:
[x,y,z]=peaks(30);
mesh(x,y,z);
grid on;

边栏推荐
- Advanced multithreading (Part 2)
- LeetCode_ 6124_ The first letter that appears twice
- SQL file import database - Nanny level tutorial
- Simple use of mongodb database
- [LeetCode周赛复盘] 第 303 场周赛20220724
- Grafana connection tdengine reports an error 535
- Detailed usage of iperf
- 自动化测试系列-Selenium三种等待详解
- Educational events
- 启牛商学院靠谱吗?讲课老师推荐开华泰账户安全吗
猜你喜欢

Soft test --- fundamentals of programming language (Part 2)

UART

C language: deep analysis function stack frame

WPF implements RichTextBox keyword query highlighting

Simple operation K6
![[leetcode weekly replay] 303rd weekly 20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)
[leetcode weekly replay] 303rd weekly 20220724
![[mindspore] [training warning] warning when executing training code](/img/5a/978889301bdd02d6474c6e5723ad97.jpg)
[mindspore] [training warning] warning when executing training code

UART

Exception, import package and file operation

Uncaught typeerror: cannot read properties of null (reading 'append') solution
随机推荐
[mindspore] [training warning] warning when executing training code
Leetcode 0123. the best time to buy and sell stocks III: dynamic programming + simulation in constant space
[LeetCode周赛复盘] 第 83 场双周赛20220723
【无标题】
MySQL的最左前缀原则
[acwing weekly rematch] 61st weekly 20220723
C # "learning code snippet" - recursively obtain all files under the folder
Simple use of mongodb database
2022 Henan Mengxin League game 2: Henan University of technology K - Rice
Two numbers that appear only once in the array
Heap and stack in embedded development
Dpdk based basic knowledge sorting-01
[英雄星球七月集训LeetCode解题日报] 第24日 线段树
[Bert] transformer/bert/attention interview questions and answers
Financial RPA robot enables enterprises to open a new era of intelligence
[untitled]
Solution to the shortest Hamilton path problem
Digital signal processing synthesis matlab design of dual tone multi frequency dialing system
Advanced multithreading (Part 2)
在混合云中管理数据库:八个关键注意事项