MATLAB Syntax
Recommended Post: 【Computer Science】 Computer Science Index
1. Overview
2. Syntax
3. Shortcuts
1. Overview
⑴ MATLAB stands for matrix laboratory
⑵ A commercial license (paid) is required to use MATLAB commercially: Contrasts with free Python
⑶ Comparison with Python
① 1-indexed
② Range end is inclusive
③ Uses “()” instead of “[]”
④ Functions do not require return: Output variables are returned automatically
2. Syntax
○
;
(semicolon): Executes the code but does not display the output. Can be used to write multiple commands in one line
○
[v d]
= version
○
%
: Comment
○
help plot
: Outputs documentation related toplot
○
disp(x)
: Displays x
○
clear
: Clears the workspace
○
clc
: Clears the command window
○
a1 = [1 2 3 4 5]
○
a2 = 1:100
○
a3 = 0:5:100
○
.*
: element-wise multiplication
○
.^
: element-wise power
○
c1 = [1; 3; 5; 7; 9]
: column vector
○
m1 = [1 2 3 ; 4 5 6 ; 7 8 9]
○
m2 = zeros(3, 2)
○
m3 = ones(3, 2)
○
m4 = rand(3, 2)
: Creates a 3 × 2 matrix by randomly sampling from a uniform range [0, 1]
○
m5 = randn(3, 2)
: Creates a 3 × 2 matrix by randomly sampling from a normal distribution N(0, 1)
○
m6 = eye(3)
○
inv(m1)
: Inverse of m1
○
m1'
: Transpose of m1
○
size(m6)
: Returns the shape of matrix m6
○
a /b
: Unlike a / b, results in an error
○
length(m1)
: Outputs the largest dimension
○
numel(m1)
: Number of elements in m1
○
m1(:)
: Flattens the m1 matrix
○
data(data > 0)
: Outputs elements greater than 0 indata
○
find(data > 0)
: Outputs indices of elements greater than 0 indata
○
hold on
: Used before plotting new graphs to display multiple graphs at once
○
subplot(2,2,2)
: Specifies the number of rows, number of columns, and index to display multiple subplots simultaneously
○
save('data.mat','data','w','x','y','t')
: Saves multiple variables into a file nameddata.mat
at once
○
save('data.mat','b','-append')
: Updates existingdata.mat
with a new variableb
○
load('data.mat')
: Loads a saved file
○
fft(y)
: Fourier Transform of signal y
○
lowpass(y, cutoff_frequency, sampling_frequency)
: Applies a lowpass filter to signal y
○
highpass(y, cutoff_frequency, sampling_frequency)
: Applies a highpass filter to signal y
○
bandpass(y, [low_cutoff_frequency high_cutoff_frequency], sampling_frequency)
: Applies a bandpass filter to signal y
○
gray_image = imread('dark_woods.tif')
: Reads an image file
○
imshow(gray_image)
: Visualizes the image
○
gray_image_equalized = histeq(gray_image)
: Histogram equalization function. Widens the distribution of pixel intensity, improving image contrast
○
colored_image_eq = cat(3, red_channel_eq, green_channel_eq, blue_channel_eq)
: Concatenate
○
rng(42)
: Random seed setting
○
cv = cvpartition(y, 'Holdout', 0.2)
: Partition data into 80:20
○
X_train = X(training(cv), :)
○
y_train = y(training(cv))
○
X_test = X(test(cv), :)
○
y_test = y(test(cv))
3. Shortcuts
○ Use Ctrl + - (Windows/Linux) or Cmd + - (macOS) to reduce font size
Input: 2024.08.26 17:14