Here are some useful things
Basics
Command | Description |
clc | Clears the command window |
clear | Clears all variables from workspace |
close all | Closes all figure windows |
who | Lists variables in workspace |
whos | Lists variables with details |
help <function> | Displays help for a function |
doc <function> | Opens documentation |
format short/long | Controls display precision |
Operators
Arithmetic Operators
Operator | Description | Operator | Description | | |
+ | Addition | - | Subtraction | | |
* | Multiplication | ^ | Power | | |
.* | Element-wise multiplication | .^ | Element-wise power | | |
/ | Right division | \ | Left division | | |
./ | Element-wise division | | | | |
Relational Operators
Operator | Description |
== | Equal |
~= | Not equal |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
Logical Operators
Operator | Description |
& | AND (element-wise) |
&& | AND (Short-circuit) |
| | OR (element-wise) |
|| | OR (Short-circuit) |
~ | NOT |
Common Functions
Function | Description |
abs(x) | Absolute value |
sqrt(x) | Square root |
exp(x) | Exponential |
log(x) | Natural logarithm |
log10(x) | Base-10 logarithm |
sin(x), cos(x), tan(x) | Trigonometric functions |
asin(x), acos(x), atan(x) | Inverse trig functions |
mod(a,b) | Remainder after division |
floor(x), ceil(x), round(x) | Rounding functions |
Matrix Operations
Syntax | Operation |
[a b; c d] | Create matrix |
A' | Transpose |
eye(n) | Identity matrix |
zeros(m,n) | Zeros matrix |
ones(m,n) | Ones matrix |
rand(m,n) | Random matrix |
size(A) | Matrix size |
det(A) | Matrix determinant |
inv(A) | Inverse matrix |
eig(A) | Eigenvalues |
A\b | Solve Ax = b |
Plotting
Command | Description |
plot(x, y) | 2D Line plot |
scatter(x, y) | Scatter plot |
bar(x, y) | Bar chart |
hist(x) | Histogram |
xlabel('text') | X-axis label |
ylabel('text') | Y-axis label |
title('text') | Title |
legend('name1', 'name2') | Legend |
grid on/off | Toggle grid |
Control Flow
Conditional Statements
Command | Description |
if condition | If statement |
elseif condition | Else-if statement |
else | Else statement |
end | End block |
Loops
Command | Description |
for i = 1:N | For loop |
while condition | While loop |
break | Break loop |
continue | Skip iteration |
File I/O
Command | Description |
save filename.mat | Save variables to file |
load filename.mat | Load variables from file |
csvwrite('file.csv', M) | Write matrix to CSV |
csvread('file.csv') | Read CSV file |
fopen('file.txt', 'r') | Open file for reading |
fclose(fid) | Close file |
Special Constants
Constant | Value |
pi | π≈3.1416 |
inf | ∞ (Infinity) |
NaN | Not a Number |
eps | Machine precision |
Useful Shortcuts
Shortcut | Description |
Ctrl + C | Stop execution |
Ctrl + Enter | Run current section |
Ctrl + K | Comment selection |
Ctrl + Shift + K | Uncomment selection |
Ctrl + L | Clear command window |