clc | Clear command window |
clear | Clear all variables from workspace |
clear x | Clear specific variable |
close all | Close all figure windows |
who / whos | List variables / with details |
help func | Get help for function |
doc func | Open documentation |
pwd | Current directory |
cd path | Change directory |
ls / dir | List files |
addpath(folder) | Add folder to path |
save file.mat | Save workspace to file |
load file.mat | Load workspace from file |
F5 | Run script |
F9 | Run selected code |
| Ctrl + C | Interrupt execution |
| Ctrl + Enter | Run current section |
| Ctrl + R | Comment selection |
| Ctrl + T | Uncomment selection |
| Ctrl + I | Smart indent |
| Ctrl + D | Open selected function |
| Ctrl + F | Find |
| Ctrl + H | Find and replace |
Tab | Autocomplete |
Up Arrow | Previous command |
A = [1 2; 3 4] | Create 2x2 matrix |
zeros(m, n) | Matrix of zeros |
ones(m, n) | Matrix of ones |
eye(n) | Identity matrix |
rand(m, n) | Random matrix (uniform) |
randn(m, n) | Random matrix (normal) |
linspace(a, b, n) | n evenly spaced points |
logspace(a, b, n) | n logarithmically spaced points |
a:step:b | Range with step |
diag(v) | Create diagonal matrix |
reshape(A, m, n) | Reshape matrix |
[A B] / [A; B] | Concatenate horizontal/vertical |
A' | Transpose |
A * B | Matrix multiplication |
A .* B | Element-wise multiplication |
A ./ B | Element-wise division |
A .^ n | Element-wise power |
A \ b | Solve linear system Ax=b |
inv(A) | Matrix inverse |
det(A) | Determinant |
eig(A) | Eigenvalues & eigenvectors |
rank(A) | Matrix rank |
trace(A) | Sum of diagonal elements |
norm(A) | Matrix norm |
A(i, j) | Element at row i, column j |
A(i, :) | Entire row i |
A(:, j) | Entire column j |
A(end, :) | Last row |
A(1:3, :) | First 3 rows |
A(:) | All elements as column vector |
A(A > 5) | Elements greater than 5 |
size(A) | Dimensions of matrix |
length(A) | Largest dimension |
numel(A) | Number of elements |
abs(x) | Absolute value |
sqrt(x) | Square root |
exp(x) | Exponential (e^x) |
log(x) / log10(x) | Natural / base-10 log |
log2(x) | Base-2 logarithm |
floor(x) / ceil(x) | Round down / up |
round(x) | Round to nearest |
mod(a, b) | Modulo |
sign(x) | Sign of number |
factorial(n) | Factorial |
sin(x) / cos(x) / tan(x) | Trigonometric functions |
asin(x) / acos(x) / atan(x) | Inverse trig functions |
atan2(y, x) | Four-quadrant arctangent |
sinh(x) / cosh(x) / tanh(x) | Hyperbolic functions |
deg2rad(x) / rad2deg(x) | Convert degrees/radians |
sum(A) | Sum of elements |
mean(A) | Average |
median(A) | Median |
std(A) | Standard deviation |
var(A) | Variance |
max(A) / min(A) | Maximum / minimum |
cumsum(A) | Cumulative sum |
diff(A) | Differences between elements |
sort(A) | Sort elements |
unique(A) | Unique elements |
plot(x, y) | Basic 2D line plot |
plot(x, y, 'r--') | Red dashed line |
scatter(x, y) | Scatter plot |
bar(x, y) | Bar chart |
barh(x, y) | Horizontal bar chart |
histogram(data) | Histogram |
pie(x) | Pie chart |
area(x, y) | Area plot |
errorbar(x, y, err) | Error bar plot |
polarplot(theta, rho) | Polar plot |
plot3(x, y, z) | 3D line plot |
surf(X, Y, Z) | 3D surface plot |
mesh(X, Y, Z) | 3D mesh plot |
contour(X, Y, Z) | Contour plot |
contourf(X, Y, Z) | Filled contour plot |
scatter3(x, y, z) | 3D scatter plot |
[X, Y] = meshgrid(x, y) | Create coordinate grid |
subplot(m, n, p) | Create subplot grid |
hold on / off | Keep/clear current plot |
title('text') | Add title |
xlabel('text') | X-axis label |
ylabel('text') | Y-axis label |
legend('a', 'b') | Add legend |
grid on / off | Toggle grid |
axis([xmin xmax ymin ymax]) | Set axis limits |
xlim([min max]) | Set x-axis limits |
ylim([min max]) | Set y-axis limits |
colorbar | Add colorbar |
colormap(jet) | Set colormap |
figure | New figure window |
saveas(gcf, 'file.png') | Save figure |
if condition ... end | If statement |
if ... elseif ... else ... end | If-elseif-else |
for i = 1:n ... end | For loop |
while condition ... end | While loop |
switch var case val ... end | Switch statement |
break | Exit loop |
continue | Skip to next iteration |
return | Return from function |
function y = f(x) | Define function |
function [a, b] = f(x) | Multiple outputs |
f = @(x) x.^2 | Anonymous function |
feval(f, args) | Evaluate function |
nargin / nargout | Number of arguments |
varargin / varargout | Variable arguments |
% comment | Single-line comment |
%{ ... %} | Block comment |
%% | Section break |
try ... catch ... end | Error handling |
error('message') | Throw error |
warning('message') | Display warning |
assert(cond, 'message') | Assert condition |
dbstop if error | Stop on error (debug) |
% Read data from file
data = readmatrix('file.csv');
% Anonymous function
f = @(x) x.^2 + 2*x + 1;
% Find indices where condition is true
idx = find(A > 5);
% Apply function to each element
result = arrayfun(@(x) x^2, A);
% Solve linear system Ax = b
x = A \ b;
% Vectorized operations (faster than loops)
y = sin(x) .* exp(-x);
% Preallocate array for speed
result = zeros(1, n);
for i = 1:n
result(i) = i^2;
end tic and toc to measure execution time; at line end to suppress outputfprintf for formatted outputdbstop if error to debug errors