当前位置:网站首页>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;

边栏推荐
- asp adodb.stream对象的方法属性
- C language: deep analysis function stack frame
- The number of palindromes in question 9 of C language deduction. Two pointer array traversal method
- Dpdk based basic knowledge sorting-01
- Codeworks round 649 (Div. 2) ABC problem solution
- [leetcode weekly replay] game 83 biweekly 20220723
- BGP machine room and BGP
- 7.24 party notice
- What are the functions of rank function
- What can testers do when there is an online bug?
猜你喜欢

WPF implements RichTextBox keyword query highlighting

How to use measurement data to drive the improvement of code review

Managing databases in a hybrid cloud: eight key considerations

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

Two numbers that appear only once in the array

C recursively obtains all files under the folder and binds them to the treeview control

Heavy forecast! Analysys, together with Microsoft and the Central University of Finance and economics, talks about the digital economy

动态规划-01背包滚动数组优化

Use es to realize fuzzy search and search recommendation of personal blog

Principle of data proxy
随机推荐
C language force buckle the eleventh question to find the maximum capacity of the bucket. (two methods)
[LeetCode周赛复盘] 第 83 场双周赛20220723
asp rs.open sql,conn,3,1中3,1代表什么?
Daily question 1 · 1260. Two dimensional network migration · simulation
Netease game Flink SQL platform practice
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
Heap and stack in embedded development
Redis 事务学习有感
R language uses ISNA function to check whether the list and dataframe contain missing values, marks abnormal values in data columns in dataframe as missing values Na, and uses na.omit function to dele
UART
Promtool Check
Docker container Django + MySQL service
Internal network mapping port to external network
Detailed explanation of the usage of vector, queue and stack
Redis memory analysis tool RMA usage
Uncaught typeerror: cannot read properties of null (reading 'append') solution
"Usaco2006nov" corn fields solution
EF core :自引用的组织结构树
[Bert] QA, reading comprehension, information retrieval
Simple operation K6