Twenty minutes to get started with Matlab

Twenty minutes to get started with Matlab

Matlab is indeed a powerful modeling software. If you already have some computer language foundation before this, then the next challenge will be pediatrics for you. Next, I want to use a simple language to sort out the most commonly used basic grammar of Matlab.

Basic concept

Write comments generally use %, and write comments directly afterwards, which will not affect the operation of the code

%hello world

The two clears clear all the variables; clc is all the contents of the screen, but the variable is not changed. The
variable assignment here is to write the name, and then use the equal sign to directly give the number

Matlab=66.66
Matlab =
   66.66

The string encloses the sentence you want to use in quotation marks

a='hello world'
a =
hello world

Matrices and arrays

Because Matlab is a software closely related to mathematics, matrices and arrays can be said to be the characteristics of Matlab or one of its core functions.
The array simply comes with a square bracket outside of the water, used between elements, separated, if you want to branch; to achieve

%数组
a=[1,2,3,4,5,6]
a =

   1   2   3   4   5   6

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

   1   2   3
   4   5   6

%上面这个叫结构数组,这里补充一个细胞数组,他可以把不同的数据类型放在一起
a={
    
    [1,2,3,4],'good';33,'bad'}
a =
{
    
    
  [1,1] =

     1   2   3   4

  [2,1] = 33
  [1,2] = good
  [2,2] = bad
}

Matrix Matrix is ​​a commonly used form in mathematics. There are several methods to automatically generate matrices commonly used in Matlab. Just check Baidu for other methods when you want to use them. % Create matrix
zeros (all zeros), ones (all ones), eye (zeros on the middle and two sides), rand (random), randn (with negative random)

%矩阵
zeros(3,3)
ans =
   0   0   0
   0   0   0
   0   0   0
ones(3,3)
ans =
   1   1   1
   1   1   1
   1   1   1
   eye(3,3)
ans =
   1   0   0
   0   1   0
   0   0   1
rand(3,3)
ans =
   0.8881   0.7350   0.8788
   0.9488   0.5038   0.1468
   0.6676   0.4057   0.6665
randn(3,3)
ans =
   0.8830   0.2095   0.3280
   0.5720  -1.0492   2.3335
   0.2689   0.3328  -0.1323

Index operation if you want to know how a particular array matrix do? Just use it in parentheses to write a few rows and columns; if you want to extract the first few rows and columns, you can use:, 2:4, 1:3 this means 2 to 4 rows, 1 to 3 columns. If yes: all rows and all columns

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

a(1,2)
ans = 2

a(1:2,2:3)
ans =
   2   3
   5   6

a(1:2,:)
ans =
   1   2   3
   4   5   6

Basic operations

Simple mathematical operations

3+2
ans = 5
3*2
ans = 6
3/2
ans = 1.5000
3-2
ans = 1
3**2
ans = 9

Matrix calculation,
simply add, subtract, multiply and divide the matrix

a=[1,2;3,4]
a =

   1   2
   3   4

a*3
ans =

    3    6
    9   12

a+a
ans =

   2   4
   6   8

a*a
ans =

    7   10
   15   22

a.*a
ans =

    1    4
    9   16

In addition, there are several commonly used calculation instructions, such as sum and inversion

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

   1   2   3
   4   5   6
   7   8   9
m=m'%倒置
m =

   1   4   7
   2   5   8
   3   6   9

sum(m)%求每列的和
ans =

   12   15   18

sum(m,2)%求每行的和
ans =

    6
   15
   24

diag(m)%如果是对角线可以先用diag函数求出对角线
ans =

   1
   5
   9

Flow control statements-selection, loop

The selection structure is
mainly divided into if and switch, the focus is if, which is mainly composed of if, else, elseif, end, which means that if the conditions are met, do it, otherwise look at the next step, the next one will not work, then look at the next one, all will not work It will not be executed. Note that you must end with end at the end

%if
N=0.5
n=rand %随机生成一个数
if n < N
   disp('aa')
elseif n< .3
   disp('bb')
else 
   disp('cc')
end 

%switch
switch 2
case 2
    y=4
case 4 
    y=5
end 
y =
2 


There are two commonly used loop structures , for and while, which means to do one thing repeatedly, for is to give a range and do it beyond the scope; while is to see the given conditions, do it if it meets the conditions, do not do it if it doesn't, generally They all end with end.

%1+2+···+100,即从1加到100
s=0
for i= 1:100
    s=s+i
end 
s
s=
5050

%while
n=1
while n<10
    n=n+1
end
n = 2
n = 3
n = 4
n = 5
n = 6
n = 7
n = 8
n = 9
n = 10

File I/o

Use save/load
save to save files, and save files in different formats according to different instructions

savefile =' pafile.mat';
p=rand(1,10);
q=ones(10);
save (savefile,'p','q')

load is used to reload the data

%把一个4列矩阵保存在ascii文件,然后把数据重新载入
a=magic(4);
b=ones(2,4) * -5.7;
c=[8,6,4,2];
save -ascii mydate.data a b c
clear a b c
load mydata.dat 

Read and write text,
fopen, open the file,
fscan, read data from the file,
fprintf, a write operation after the file is opened, you can write things into the file,
fclose, and close the file

%
tempstr='78.F 72.F 64.F 66.F 49.F';%这个摄氏度的小圆圈我不会打,用.代替
fid=fopen('temperature.dat','w+');
fprintf(fid,"%s',tempstr);
%返回到文件头

%读取文件中的数据
frewind(fid);
degrees=char(176)
num_temps =fscanf(fid,['%d',degrees 'F'])
num_temps =
78
72
64
66
49

Reference Materials
Proficient in Matlab scientific computing and data statistics applications-Zhao Bin, Chen Ming waiting for
Matlab from entry to practice-Xie Longhan, Cai
Siqi quick start to Matlab R2017b-after it is a guest blog post

Guess you like

Origin blog.csdn.net/weixin_47567401/article/details/113278297