Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Aaron Erhardt
Cheatsheets
Commits
5b379aed
Commit
5b379aed
authored
May 16, 2021
by
Aaron Erhardt
Browse files
Restructure Matlab cheatsheet
Signed-off-by:
Aaron Erhardt
<
aaron.erhardt@t-online.de
>
parent
c97b2866
Changes
1
Hide whitespace changes
Inline
Side-by-side
Matlab/Matlab.md
View file @
5b379aed
# Mat
lab cheatsheet
# Mat
rices
##
Matrices
##
Generate
### Generate
Manual
### Manual
```
matlab
A
=
[
1
2
3
;
4
5
6
]
...
...
@@ -14,41 +12,41 @@ A =
4
5
6
```
Zero
###
Zero
```
matlab
A
=
zeros
(
3
)
% 3x3
B
=
zeros
(
3
,
2
)
% 3x2
```
Ones
###
Ones
```
matlab
A
=
ones
(
3
)
% 3x3
B
=
ones
(
3
,
2
)
% 3x2
```
Eye (zeros with ones in the diagonal)
###
Eye (zeros with ones in the diagonal)
```
matlab
A
=
eye
(
3
)
% 3x3
B
=
eye
(
3
,
2
)
% 3x2
```
Magic
###
Magic
```
matlab
A
=
magic
(
3
)
% 3x3
```
Random values (from 0 to 1)
###
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)
###
Normally distributed random values (σ = 1, µ = 0)
```
matlab
A
=
randn
(
3
)
.*
2
% 3x3 with σ = 2, µ = 0
...
...
@@ -56,90 +54,82 @@ B = randn(3, 2) % 3x2
C
=
randn
(
3
,
2
)
+
3
% 3x2 with σ = 1, µ = 3
```
##
#
Modify
## Modify
Transpose
###
Transpose
```
matlab
T
=
A
'
```
Invert
###
Invert
```
matlab
B
=
inv
(
A
)
```
Rotate
###
Rotate
```
matlab
B
=
rot90
(
A
)
```
Flip
###
Flip
```
matlab
B
=
flip
(
A
)
% flip rows (top to bottom)
B
=
fliplr
(
A
)
% flip columns (left to right)
```
Multiply
###
Multiply
```
matlab
C
=
A
*
B
```
Element wise multiplication
###
Element wise multiplication
```
matlab
C
=
A
.*
B
```
##
#
Access
## Access
As row vector
###
As row vector
```
matlab
A
(:)
```
Get rows
###
Get rows
```
matlab
A
(
1
,
:)
% row 1
```
Get columns
###
Get columns
```
matlab
A
(:,
1
)
% column 1
```
Get diagonal
###
Get diagonal
```
matlab
diag
(
A
)
% main diagonal (top left to bottom right)
diag
(
flip
(
A
))
% other diagonal (bottom left to top right)
```
##
#
Linear equations
## Linear equations
```
matlab
x
=
A
\
b
% solves Ax = b
```
### Visualize
Histogram
```
matlab
histogram
(
A
)
histogram
(
A
,
15
)
% place values in 15 groups
```
#
# Images
# Images
##
#
Load image
## Load image
```
matlab
img
=
imread
(
'path_to_file'
)
...
...
@@ -158,132 +148,135 @@ colormap(gray()) % gray color map
colormap
(
M
)
% where M 255x3 with values from 0 to 1
```
## Strings
### Create
# Datatypes
## Struct
```
matlab
str
=
"Hello"
fromChar
=
string
(
text
)
student
.
name
=
"TestStudent"
% create
student
.
age
=
24
% add field
student2
=
struct
(
'name'
,
'Student2'
,
'age'
,
23
)
% compact
disp
(
student
.
name
)
% print name
```
### Concatenate
```
matlab
str2
=
str
+
" World!"
```
## CellArray
## Character array
Array containing different data types
### Create
```
matlab
text
=
'Hello'
fromStr
=
char
(
str
)
ca
=
{
'chars'
,
9.81
,
1
}
% 1x3 cell array
```
###
Concatenate
###
Access elements
```
matlab
text2
=
[
text
' World!'
]
ca
(
1
)
% -> 'chars'
```
###
Access characters
###
Get length
```
matlab
c
=
text
(
1
)
% first character
length
(
ca
)
```
### Convert to number
## Table
### Properties
```
matlab
str2num
(
'2'
)
table
.
Properties
table
.
Properties
.
VariableNames
{
1
}
% first column name
```
## Output
### Create formatted string
### Access column by name
```
matlab
sprintf
(
'value: %f'
,
float_val
)
table
.
column_name
% access column (as vector) with name "column_name"
```
###
Print string
###
Get sub-table
```
matlab
disp
(
'Hello'
)
table
(:,
2
)
% second column as table
```
###
Print formatted string
###
Access values
```
matlab
% use \n for newline, the 1 is for printing to stdout
fprintf
(
1
,
'value: %d\n'
,
2
)
table
{
1
,
2
}
% row 1, column 2, note brackets!
table
{:,
2
}
% column
2
```
#
Datatype
s
#
#
String
s
##
Struct
##
# Create
```
matlab
student
.
name
=
"TestStudent"
% create
student
.
age
=
24
% add field
student2
=
struct
(
'name'
,
'Student2'
,
'age'
,
23
)
% compact
disp
(
student
.
name
)
% print name
str
=
"Hello"
fromChar
=
string
(
text
)
```
## C
ellArray
##
#
C
oncatenate
Array containing different data types
```
matlab
str2
=
str
+
" World!"
```
## Character array
### Create
```
matlab
ca
=
{
'chars'
,
9.81
,
1
}
% 1x3 cell array
text
=
'Hello'
fromStr
=
char
(
str
)
```
###
Access elements
###
Concatenate
```
matlab
ca
(
1
)
% -> 'chars'
text2
=
[
text
' World!'
]
```
###
Get length
###
Access characters
```
matlab
length
(
ca
)
c
=
text
(
1
)
% first character
```
## Table
### Properties
### Convert to number
```
matlab
table
.
Properties
table
.
Properties
.
VariableNames
{
1
}
% first column name
str2num
(
'2'
)
```
### Access column by name
## Output
### Create formatted string
```
matlab
table
.
column_name
% access column (as vector) with name "column_name"
sprintf
(
'value: %f'
,
float_val
)
```
###
Get sub-table
###
Print string
```
matlab
table
(:,
2
)
% second column as table
disp
(
'Hello'
)
```
###
Access values
###
Print formatted string
```
matlab
table
{
1
,
2
}
% row 1, column 2, note brackets!
table
{:,
2
}
% column
2
% use \n for newline, the 1 is for printing to stdout
fprintf
(
1
,
'value: %d\n'
,
2
)
```
# Save and load
## Workspace
...
...
@@ -379,6 +372,13 @@ fplot(@sin, [-pi pi]); % from -pi to pi
## Charts
### Histogram
```
matlab
histogram
(
A
)
% where A is a vector of values
histogram
(
A
,
15
)
% place values in 15 groups
```
### Bar
```
matlab
...
...
@@ -469,6 +469,7 @@ axis([XMIN XMAX YMIN YMAX]); % set scaling of both axes
```
matlab
fig
=
figure
;
% note: properties are case sensitive!
fig
=
gcf
;
% get handle to current figure
clf
;
% clear current figure
```
### Typical object hierarchy
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment