MATLAB Cheat Sheet updating
Basic commands
Commands | Description |
---|---|
clc |
Clear command window |
clear |
Clear system memory |
clear x |
Clear x from memory |
doc normally distributed numbers |
Searching the documentation for a function that creates normally distributed numbers |
Standard Matrix and vector operations
Commands | Description |
---|---|
x = [1, 2, 3] or x=[1 2 3] |
1x3 (Row) vector defined |
x = [1; 2; 3] |
3x1 (Column) vector defined |
x = [1, 2, 3; 4, 5, 6; 7, 8, 9] |
3x3 matrix defined |
x = 1:3 or x = 1:1:3 |
1x3 (Row) vector defined |
x = linspace(1,3,3) |
function to create vector linspace(_first_,_last_,_number_of_elements_) |
x = x' |
Transpose x from a row vector to a column vector using the transpose operator ' |
x = rand(3,3) or x = ones(3,3) or x = zeros(3,3) |
Create a 3-by-3 matrix with (random, one, zero) numbers |
size(x) |
Get Rows and Columns |
x(1,:) |
Get first row of x matrix |
x(:,1) |
Get first colum of x matrix |
x(end-1,1) |
Get the second last row and 1st column |
A = [5 6; 7 8] A(3) |
If you use only one index with a matrix, MATLAB traverses down each column in order (this code returns the value 6 ) |
density([1 3 6]) |
Indices can be nonconsecutive numbers to extract the first, third, and sixth elements of density |
Operators
Operator | Purpose | Description |
---|---|---|
+ |
Addition | A+B adds A and B |
+ |
Unary plus | +A rutruns A |
- |
Subtraction | A-B subtracts B from A |
- |
Unary minus | -A negates the elements of A |
.* |
Element-wise multiplication | A.*B is the element-by-element product of A and B . |
.^ |
Element-wise power | A.^B is the matrix with elements A(i,j) to the B(i,j) power. |
./ |
Right array division | A./B is the matrix with elements A(i,j)/B(i,j) . |
.\ |
Left array division | A.\B is the matrix with elements B(i,j)/A(i,j) . |
.' |
Array transpose | A.' is the array transpose of A . For complex matrices, this does not involve conjugation. |
~ |
Ignore output from a function | [~,ivMax] = max(v2) ~ ignore the first output from max() function |
MATLAB Cheat Sheet updating
https://github.com/kewending/kewending.github.io/2024/04/02/MATLAB Cheat Sheet/