BME | EEG Analysis - Matlab Basics
Matlab Basics
size(x)
: Dimensions of matrix x.
length(x)
: Maximum length across all dimensions of x.
zeros(1,2)
: Generates a matrix of zeros with 1 row and 2 columns.
ones(2,3)
: Generates a matrix of ones with 2 rows and 3 columns.
eye(2)
: Creates a 2x2 identity matrix with 1s on the diagonal and 0s elsewhere.
round(x)
: Rounds x to the nearest integer.
ceil(x)
: Rounds x up to the nearest integer.
floor(x)
: Rounds x down to the nearest integer.
repmat([1,2],2, 3)
: Replicates the array [1,2] into a 2x3 matrix, such as [1,2,1,2,1,2; 1,2,1,2,1,2].
diff(x)
: Calculates the forward difference of x, i.e., the difference between consecutive elements.
Logical operators: >
, <
, ==
, ~=
, &&
, ||
.
Function
1 | % Define a function |
for Loop
1 | for i = 1:size(EEG,1) |
Space Allocation
1 | filteredEEG = zeros(size(EEG)); % Allocate space beforehand to reduce memory usage |
Data Type: Cell
1 | image{1} = 'bear'; |
Data Type: structure
1 | subject(1).label = 'S1'; |
Multidimensional Matrix
1 | EEG(:,:,1); % Outputs the first page |
📌 How to improve Matlab’s computational speed?
- Matrix initialization\
- Prefer matrix operations, avoid loops\
- Store in row form rather than column form, i.e., [1,2,3,…] instead of [1;2;3;…]\
- Use Matlab profiler to identify time-consuming segments.
Disclaimer: This blog content serves as class notes, shared for educational purposes. Some images and content are sourced from textbooks, lecture materials, and the internet. If there are any copyright concerns, please contact aursus.blog@gmail.com for removal.
Translation is done by GPT.