Summary of matlab notes (3)

MATLAB software and algorithm realization

1.如何调整自己的工作区
2.使用matlab时的注意事项,比如不能按CTRL+Q
3.语句后面加分号,不是交互式,不加分号,会同时打印结果 即交互式。
4.字符串使用单引号才可以。
5.变量赋值不需要提前声明,随时使用。
6.clear清除变量,clc清除屏幕。
7.who和whos查看我们已经拥有的变量的各项树形。
类型强制转换。

1. Basics of MATLAB programming

MATLAB is a widely used, rapidly developing, and powerful mathematical and engineering calculation software with matrix as the basic unit of operation. Let's start with the understanding of MATLAB software.

Millions of engineers and scientists around the world use MATLAB® to analyze and design the systems and products that change our world. The matrix-based MATLAB language is the most natural way in the world to express computational mathematics. Data can be easily visualized and drilled down using built-in graphs. You are welcome to experiment, explore, and discover with the desktop environment. These MATLAB tools and functions have all been rigorously tested to work with each other.

MATLAB helps you keep your ideas beyond the desktop. You can run analytics on large datasets and scale to clusters and clouds. MATLAB code can be integrated with other languages, enabling you to deploy algorithms and applications across the web, enterprise, and production systems.

clear: delete the variable.
clc: Clears the contents of the screen, but does not clear existing variables.

insert image description here

There are 5 windows on the main interface: main window, command window, current directory window, workspace window, and command history window. We mainly use the main window and command window, and other windows are auxiliary. The command window is where we interact with the system. Let's look at an example first:

Example: Calculation, that is, 123 to the 45th power. It is very easy to solve this problem with MATLAB, just enter 123^45 directly in the command window, and then press Enter, the result is 1.1110e+094, which means.

In order to carry out more complex calculations, we discuss the general method of MATLAB programming.

1.1 Basic operations of MATLAB and basic operations of matrices

Commonly used management commands for workspaces are:

(1) who and whos (list all variables in the workspace): query command

(2) clear: Clear all variables in the workspace

 clear var1 var2:清除工作空间中的变量var1和var2 
 clc: 清理窗口

(3) save FileName: save all memory variables as Filename.mat file

 save FileName v1 v2:把变量v1和v2保存为Filename.mat文件
 save FileName v1 v2 –append:把变量v1和v2添加到Filename.mat文件

(4) load FileName: transfer all variables in the Filename.mat file into the memory
load FileName v1 v2: transfer v1 and v2 in the Filename.mat file into the memory

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-yoIWkcIi-1681128933125)(2023-04-10-19-32-10.png)]

connection string
insert image description here
insert image description here

who whos
insert image description here

(If we split the line with ;, then each individual statement will be executed separately)

In the chestnuts above, we can clearly see the difference between who and whos. The who command only lists the names of variables, while the whos command displays more comprehensive information, including the size of the data (because matlab uses two by default. Dimensional array storage, so for numbers, it is an array of one row and one column, which is a single value), type, how much space it occupies, and other data.

insert image description here

baseNum = 123.456;
toUint8 = uint8(baseNum);
toUint32 = uint32(baseNum);%这个是注释的符号%
whos;

​​Desktop basics

When starting MATLAB®, the desktop is displayed in the default layout.


桌面包括下列面板:


使用 MATLAB 时,可发出创建变量和调用函数的命令。例如,通过在命令行中键入以下语句来创建名为 a 的变量:

a = 1
MATLAB 将变量 a 添加到工作区,并在命令行窗口中显示结果。

a = 1
创建更多变量。

b = 2
b = 2
c = a + b
c = 3
d = cos(a)
d = 0.5403

如果未指定输出变量,MATLAB 将使用变量 ans(answer 的缩略形式)来存储计算结果。

sin(a)
ans =0.8415

如果语句以分号结束,MATLAB 会执行计算,但不在命令行窗口中显示输出。

e = a*b;
按向上 (↑) 和向下箭头键 (↓) 可以重新调用以前的命令。在空白命令行中或在键入命令的前几个字符之后按箭头键。例如,要重新调用命令 b = 2,请键入 b,然后按向上箭头键。

2. Generation of matrix

The operation object of MATLAB is a matrix. Scalars can be viewed as 1-by-1 matrices, and dimensional row or column vectors can be viewed as or-dimensional matrices.
The basic format of row vector generation: start: step: end (step is 1 by default)
For example: in the command window, enter after the prompt >>, x=1:5, press Enter, and the displayed output results are as follows :

x =
1 2 3 4 5

After each command of Matlab, if there is a comma "," or no punctuation mark, the result of the command will be displayed; if the command is followed by a semicolon ";", the display of the result is prohibited, and "%" is used to realize the comment function.

Vectors can also be generated using the following two functions.

linspace: Linear bisection vector, for example, a=linspace(0,2*pi,100).

logspace: Logarithmically divided vector, for example, a=logspace(0,2*pi,100).

In the input of the matrix, the convention: (1) Separate the elements with spaces or commas;

(2) Enclose all elements with square brackets [ ];

(3) Use a semicolon (;) to indicate the end of the line.
In the command window, when inputting a matrix, press Shift+Enter to start inputting a new line, and all lines are required to have the same columns.
For example: a=[1 2 3; 4,5 6; 7 8,9]
This matrix is ​​always saved in the workspace until it is modified.

insert image description here

%程序文件gex118.m
clc,cleare
8(1);
%控制随机数生成器,进行一致性比较
al=and(5,2,3):
%生成2×3的[1,5]上的随机整数矩阵
a2=xand([0,6],2,3);
%生成2×3的[0,6]上的随机整数矩阵
a3-randi(5,3);
%生成3×3的[1,5]上的随机整数矩阵
a4=xand([0,6],3);
%生成3×3的[0,6]上的随机整数矩阵
a=[al,a2;a3,a4]
%构造分块矩阵
b=a(:,[end:-1:l])
%对矩阵的列进行逆序变换
b(end,:)=[]
%删除矩阵b的最后一行
c=b(:%逐列展开矩阵b的列形成一个长的列向量~
d=triu(a4,1)
%截取主对角线以上元素构成的矩阵

insert image description here

Guess you like

Origin blog.csdn.net/shaozheng0503/article/details/130068838