MATLAB Cheatsheet - MATLAB Commands & Matrix Operations Reference

This reference is for engineers and scientists who think in matrices rather than element loops. It covers building matrices and vectors, in-place operations like transpose and eigenvalue decomposition, statistical summaries, plotting curves and surfaces, file read/write, and FFT-based signal analysis. Unlike numpy the commands here feel native to the language, so you can type them directly in the Command Window and inspect each result. After reading you should be able to assemble a matrix, transpose/invert/decompose it, turn a vector into a plot, and save the intermediate result with save/load.

Languages·83 commands·Last updated 2026-07-21
matlabScientific ComputingMatrices

Matrix Creation 8

A = [1 2 3; 4 5 6; 7 8 9]
Create a 3x3 matrix
B = [1; 2; 3]
Create a column vector
C = [1, 2, 3]
Create a row vector
D = zeros(3, 4)
Create a 3x4 zero matrix
E = ones(2, 3)
Create a 2x3 ones matrix
F = eye(3)
Create a 3x3 identity matrix
G = rand(3, 3)
Create a 3x3 random matrix
H = linspace(0, 10, 100)
100 evenly spaced points from 0 to 10

Matrix Operations 11

A'
Matrix transpose
inv(A)
Matrix inverse
det(A)
Matrix determinant
rank(A)
Matrix rank
trace(A)
Matrix trace (sum of diagonal)
eig(A)
Eigenvalues and eigenvectors
svd(A)
Singular value decomposition
A * B
Matrix multiplication
A .* B
Element-wise multiplication
A / B
Right division (A * inv(B))
A \ B
Left division (inv(A) * B)

Data Visualization 16

plot(x, y)
Plot a 2D curve
scatter(x, y)
Scatter plot
bar(x, y)
Bar chart
hist(data)
Histogram
pie(data)
Pie chart
surf(X, Y, Z)
3D surface plot
mesh(X, Y, Z)
3D mesh plot
contour(X, Y, Z)
Contour plot
title("Chart Title")
Set the chart title
xlabel("X Axis")
Set the X-axis label
ylabel("Y Axis")
Set the Y-axis label
legend("Curve 1", "Curve 2")
Add a legend
grid on
Show grid
hold on
Hold the current figure and keep drawing
hold off
Release the figure hold
subplot(2, 2, 1)
Create the 1st of a 2x2 subplot grid

Data Processing 12

mean(A)
Compute the mean
median(A)
Compute the median
std(A)
Compute the std deviation
var(A)
Compute the variance
max(A)
Compute the max
min(A)
Compute the min
sum(A)
Compute the sum
cumsum(A)
Compute the cumulative sum
diff(A)
Compute differences
sort(A)
Sort
unique(A)
Unique values
find(A > 5)
Find indices meeting a condition

File I/O 10

load("data.mat")
Load a MAT file
save("data.mat", "var")
Save variables to a MAT file
data = csvread("file.csv")
Read a CSV file
csvwrite("file.csv", data)
Write a CSV file
data = xlsread("file.xlsx")
Read an Excel file
xlswrite("file.xlsx", data)
Write an Excel file
fid = fopen("file.txt", "r")
Open a text file
fclose(fid)
Close a file
fscanf(fid, "%f", [1, inf])
Read file contents
fprintf(fid, "%f\n", data)
Write file contents

Programming Basics 10

for i = 1:10 ... end
for loop
while condition ... end
while loop
if condition ... elseif ... else ... end
Conditional (if/elseif/else)
switch expr case val1 ... otherwise ... end
switch statement
function [out] = func(in) ... end
Define a function
% comment
Single-line comment
%{ block comment %}
Block comment
disp("message")
Display a message
fprintf("format %d\n", var)
Formatted output
input("Prompt: ")
User input

Linear Algebra 9

A * x = b
Solve a linear system
x = A \ b
Solve via left division
[L, U, P] = lu(A)
LU decomposition
[Q, R] = qr(A)
QR decomposition
[U, S, V] = svd(A)
SVD
norm(A)
Matrix norm
cond(A)
Condition number
null(A)
Null space basis
orth(A)
Orthogonal basis

Signal Processing 7

fft(x)
Fast Fourier transform
ifft(X)
Inverse FFT
conv(x, y)
Convolution
xcorr(x, y)
Cross-correlation
filter(b, a, x)
Digital filtering
spectrogram(x)
Spectrogram
psd(x)
Power spectral density

Tips

  • MATLAB indices start at 1, not 0 (unlike Python/C).
  • Matrix multiply uses *; element-wise uses .* - keep them distinct.
  • plot connects points into a curve; scatter draws individual points.
  • hold on overlays multiple curves on one figure; hold off releases it.
  • load/save default to MAT format; use csvread/csvwrite for CSV.
  • A function file must match the function name, e.g. myfunc.m for function myfunc.
  • Use help to view function docs, e.g. help plot.
  • Use doc to open full documentation, e.g. doc plot.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us