Matlab基本操作
save datafile 保存文件
clear 清除数据
load filename 加载文件
clc 清楚命令行
.mlx 文件 实时编辑器文件
x=1:4
x = 20:2:26
linspace(first,last,number_of_elements)
x = x' matrix transpose
x = rand(5)
x = rand(5,1)
x = ones(2,3)
x = zeros(6,3)
size(x)
rand(size(x)) 嵌套
获取矩阵元素
x=data(6,3)
y = A(end-1,end-2)
density=data(:,2)
volumes=data(:,end-1:end) Create a variable volumes containing the last two columns of data.
p=density(2:5)
y=8
x=data(y) 按列查找第8个元素
The * operator performs matrix multiplication. So, if you use * to multiply two equally sized vectors, since the inner dimensions do not agree, you will get an error message.
z = [3 4] * [10 20]
Error using *
Incorrect dimensions for matrix multiplication.
In contrast, the .* operator performs elementwise multiplication and allows you to multiply the corresponding elements of two equally sized arrays.
z = [3 4] .* [10 20]
z =
30 80
奇奇怪怪的运算:
https://www.mathworks.com/help/matlab/matlab_prog/compatible-array-sizes-for-basic-operations.html
dsize=size(data) 结果 7 4
[dr,dc]=size(data) 结果 dr=7 dc=4
[vMax, ivMax]=max(v2) 结果 vMax最大值,ivMax对应的索引
If you only need the second output from a function, you can use a tilde (~) to ignore specific outputs.
For example, you might only want the index containing the maximum value in a vector:
density = data(:,2)
[~,ivMax] = max(v2)
densityMax = density(ivMax)
doc randi 帮助命令
Two vectors of the same length can be plotted against each other using the plot function.
plot(x,y)
The plot function accepts an additional argument that allows you to specify the color, line style, and marker style using different symbols in single quotes.
plot(x,y,"r--o")
The command above plots a red (r) dashed (--) line with a circle (o) as a marker.
https://www.mathworks.com/help/matlab/ref/linespec.html
Notice that each plot command created a separate plot. To plot one line on top of another, use the hold on command to hold the previous plot while you add another line.
plot(x1,y1)
hold on
plot(x2,y2)
While the hold state is on, plots will continue to go on the same axes. To return to the default plot behavior, where each plot gets its own axes, enter hold off.
Plot v1 (y-axis) against sample (x-axis) with red (r) circle (o) markers and a solid line (-). Use a line width of 4.
plot(sample,v1,"r-o","lineWidth",4)
https://matlabacademy.mathworks.com/R2021a/portal.html?course=gettingstarted#chapter=9&lesson=1§ion=1
Labels can be added to plots using plot annotation functions, such as title. The input to these functions is a string. Strings in MATLAB are enclosed in double quotes (").
title("Plot Title")
Y坐标注释
ylabel("Mass(g)")
标注
legend("Exp A","Exp B")