Commit d309fc2d authored by Aaron Erhardt's avatar Aaron Erhardt 🦀
Browse files

Add Matlab cheatsheet

parent 0f31e336
# Matlab cheatsheet
## Matrices
### Generate
+ Manual
```matlab
A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6
```
+ Zero
```matlab
A = zeros(3) % 3x3
B = zeros(3, 2) % 3x2
```
+ Ones
```matlab
A = ones(3) % 3x3
B = ones(3, 2) % 3x2
```
+ Eye (zeros with ones in the diagonal)
```matlab
A = eye(3) % 3x3
B = eye(3, 2) % 3x2
```
+ Magic
```matlab
A = magic(3) % 3x3
```
+ Random values (from 0 to 1)
```matlab
A = rand(3) .* 2 % 3x3 with values from 0 to 2
B = rand(3, 2) % 3x2
```
+ Normally distributed random values (σ = 1, µ = 0)
```matlab
A = randn(3) .* 2 % 3x3 with σ = 2, µ = 0
B = randn(3, 2) % 3x2
C = randn(3, 2) + 3 % 3x2 with σ = 1, µ = 3
```
### Modify
+ Transpose
```matlab
T = A'
```
+ Invert
```matlab
B = inv(A)
```
+ Rotate
```matlab
B = rot90(A)
```
+ Flip
```matlab
B = flip(A) % flip rows (top to bottom)
B = fliplr(A) % flip columns (left to right)
```
+ Multiply
```matlab
C = A * B
```
+ Element wise multiplication
```matlab
C = A .* B
```
### Access
+ As row vector
```matlab
A(:)
```
+ Get rows
```matlab
A(1, :) % row 1
```
+ Get columns
```matlab
A(:, 1) % column 1
```
+ Get diagonal
```matlab
diag(A) % main diagonal (top left to bottom right)
diag(flip(A)) % other diagonal (bottom left to top right)
```
### Linear equations
```matlab
x = A \ b % solves Ax = b
```
### Visualize
+ Histogram
```matlab
histogram(A)
```
## Images
+ Load image
```matlab
img = imread('path_to_file')
```
+ Show image
```matlab
imagesc(img)
```
+ Color map
```matlab
colormap(gray()) % gray color map
colormap(M) % where M 255x3 with values from 0 to 1
```
## Strings
+ Create
```matlab
str = "Hello"
fromChar = string(text)
```
+ Concatenate
```matlab
str2 = str + " World!"
```
## Character array
+ Create
```matlab
text = 'Hello'
fromStr = char(str)
```
+ Concatenate
```matlab
text2 = [text ' World!']
```
+ Access characters
```matlab
c = text(1) % first character
```
### Convert
+ string to number
```matlab
str2num('2')
```
## Output
+ Create formatted string
```matlab
sprintf('value: %f', float_val)
```
+ Print string
```matlab
disp('Hello')
```
+ Print formatted string
```matlab
fprintf('value: %d\n', 1) % use \n for newline
```
# Datatypes
## Structs
+ Create struct, add fields and access fields
```matlab
student.name = "TestStudent" % create
student.age = 24 % add field
student2 = struct('name', 'Student2', 'age', 23) % compact
disp(student.name) % print name
```
## CellArray
Array containing different data types
+ Create
```matlab
ca = { 'chars', 9.81, 1 } % 1x3 cell array
```
+ Access elements
```matlab
ca(1) % -> 'chars'
```
+ Length
```matlab
lenght(ca)
```
## Table
+ Properties
```matlab
table.Properties
table.Properties.VariableNames(1) % first column name
```
+ Access column
```matlab
table.column_name
```
+ Access elements
```matlab
table{1, 2} % row 1, column 2
table{:, 2} % column 2
```
# Save and load
## Workspace
+ Save and load
```matlab
save file_name
load file_name
```
## Table
+ Load
```matlab
table = readtable('file_name')
detectImportOptions('file_name') % return detected import options
```
## File
```matlab
file_handle = fopen('file_name', 'rw') % open as readable and writable
fclose(file_handle)
```
# Control structures
## for-loop
```matlab
for cnt = 1:5 % cnt can be any row vector
fprintf('%d\n', cnt);
end
```
## while-loop
```matlab
cnt = 0;
while cnt < 5
fprintf('%d\n', cnt);
cnt = cnt + 1;
end
```
## if-statement
```matlab
cnt = 0;
if cnt == 0
disp('Zero');
elseif mod(cnt, 2) == 0
disp('Even');
else
disp('Odd');
end
```
+ Comparing vectors
```matlab
vec1 = [1 2 3];
vec2 = [1 2 4];
vec1 == vec2
% 1×3 logical array
% 1 1 0
if ~isequal(vec1, vec2)
disp('Not equal')
end
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment