MATLAB Cheatsheet - MATLAB Commands & Matrix Operations Reference
All essential MATLAB commands organized by use case, with 83+ entries you can copy and run directly. Find the right command fast when you need it.
Back to LanguagesMatrix Creation 8
A = [1 2 3; 4 5 6; 7 8 9]Create 3x3 矩阵
B = [1; 2; 3]Create列向量
C = [1, 2, 3]Create行向量
D = zeros(3, 4)Create 3x4 零矩阵
E = ones(2, 3)Create 2x3 全 1 矩阵
F = eye(3)Create 3x3 单位矩阵
G = rand(3, 3)Create 3x3 随机矩阵
H = linspace(0, 10, 100)Create 0-10 的 100 个等间距点
Matrix Ops 11
A'矩阵转置
inv(A)矩阵求逆
det(A)矩阵行列式
rank(A)矩阵秩
trace(A)矩阵迹(对角线元素之和)
eig(A)特征值和特征向量
svd(A)奇异值分解
A * B矩阵乘法
A .* B元素对应相乘
A / B矩阵右除(A * inv(B))
A \ B矩阵左除(inv(A) * B)
Visualization 16
plot(x, y)绘制二维曲线
scatter(x, y)绘制散点图
bar(x, y)绘制柱状图
hist(data)绘制直方图
pie(data)绘制饼图
surf(X, Y, Z)绘制三维曲面图
mesh(X, Y, Z)绘制三维网格图
contour(X, Y, Z)绘制等高线图
title("标题")Set图表标题
xlabel("X 轴")Set X 轴标签
ylabel("Y 轴")Set Y 轴标签
legend("曲线 1", "曲线 2")添加图例
grid onDisplay网格
hold on保持当前图形,继续绘制
hold off释放图形保持
subplot(2, 2, 1)Create 2x2 子图中的第 1 个
Data Processing 12
mean(A)计算均值
median(A)计算中位数
std(A)计算标准差
var(A)计算方差
max(A)计算max
min(A)计算min
sum(A)计算总和
cumsum(A)计算累积和
diff(A)计算差分
sort(A)sort
unique(A)dedupe
find(A > 5)Find满足condition的index
File I/O 10
load("data.mat")加载 MAT File
save("data.mat", "var")Savevariable到 MAT File
data = csvread("file.csv")Read CSV File
csvwrite("file.csv", data)Write CSV File
data = xlsread("file.xlsx")Read Excel File
xlswrite("file.xlsx", data)Write Excel File
fid = fopen("file.txt", "r")Open文本File
fclose(fid)CloseFile
fscanf(fid, "%f", [1, inf])ReadFile内容
fprintf(fid, "%f\n", data)WriteFile内容
Programming 10
for i = 1:10 ... endfor loop
while condition ... endwhile loop
if condition ... elseif ... else ... endcondition判断
switch expr case val1 ... otherwise ... endswitch 语句
function [out] = func(in) ... end定义function
% 注释单行注释
%{ 多行注释 %}多行注释
disp("message")Display消息
fprintf("格式 %d\n", var)格式化Output
input("提示: ")UserInput
Linear Algebra 9
A * x = b解线性方程组
x = A \ b左除求解
[L, U, P] = lu(A)LU 分解
[Q, R] = qr(A)QR 分解
[U, S, V] = svd(A)奇异值分解
norm(A)矩阵范数
cond(A)condition数
null(A)零空间基
orth(A)正交基
Signal Processing 7
fft(x)快速傅里叶变换
ifft(X)逆傅里叶变换
conv(x, y)卷积
xcorr(x, y)互相关
filter(b, a, x)数字滤波
spectrogram(x)频谱图
psd(x)功率谱密度
💡 Tips
- MATLAB index从 1 开始,不是从 0 开始(与 Python/C 不同)。
- 矩阵乘法用 *,元素对应乘法用 .*,note区分。
- plot function会自动Connection数据点,绘制曲线;scatter 绘制散点图。
- hold on 可以在同一图上绘制多条曲线,hold off 释放保持。
- load 和 save 默认使用 MAT 格式,csvread/csvwrite 用于 CSV File。
- functionFile必须以function名同名,如 function [out] = myfunc(in) Save为 myfunc.m。
- 使用 help 命令Showfunction帮助,如 help plot。
- 使用 doc 命令Open完整文档,如 doc plot。
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Found an error? Report it
Wrong command or description? Open an issue to help us fix it.
Found an error? Report it