MATLAB Basics (1)
Jihyun Son & Hyunjun Joe
2024.07.09
Coding and Communications Lab.
Department of Electrical Engineering
Korea Advanced Institute of Science and Technology (KAIST)
What is Matlab?
It is developed by The Mathworks, Inc. (http://www.mathworks.com)
It is an interactive, integrated, environment
• for numerical/symbolic, scientific computations and other apps.
• shorter program development and debugging time than traditional
programming languages such as FORTRAN and C.
• slow (compared with FORTRAN or C) because it is interpreted.
• automatic memory management; no need to declare arrays.
• intuitive, easy to use.
• compact notations.
2
Setting Up a Work Environment for MATLAB
1. My PC -> Local Disk (C;) -> Create a new folder (in English!!)
2. Run MATLAB -> Go to the folder you created in 1.
3. If the location of the file does not match the location shown on the left, the file
does not run.
3
Setting Up a Work Environment for MATLAB
• Create Hellomatlab.m file • Follow along and write the code as
• If you double-click the .m file, the editor shown below
appears • Shortcut key to run = F5
% Creating a vector, the space between numbers acts like a comma
a = [1 2 3 4];
% Creating a vector
b = [1 3 5; 2 4 6; 7 8 10];
% Creating a vector composed entirely of zeros
c = zeros(1,4);
% Creating a vector composed entirely of ones
d = ones(3,1);
% MATLAB can compute all elements of a matrix using a single
arithmetic operator
e = a + 1;
f = sin(b);
% Matrix transpose is done using a single quote (')
g = b';
% If you want to square all the elements of a matrix at once,
h = b.*b;
% You can also concatenate matrices or vectors.
i = [a c];
4
Setting Up a Work Environment for MATLAB
Current directory
where the Let's double-click workspace
program is variable 𝑏b to check if vectors and
matrices have been stored
running Editor correctly.
Column
Row
5
Useful Things To Know
clc – Clear the contents of
the command window
Editor - write code, execute
clear all – Clear all
(ctrl+enter) Commenting - Inserting
contents in the workspace
comments within the code,
not recognized in the
ㄴ command window (ctrl+r)
close all – Clear executed Uncomment (ctrl+t)
graphs, animations, etc.
6
Basic Operations in MATLAB
Scalars, vectors (matrices) input
Scalar : ex) a=5
ex) pi, inf(infinity), ans(recent calculation result)
Vector : Matrix input, row separation - semicolon (;) / column separation - comma (,) or
space
Row vector : a = [0,1,2,3] , a = [0 1 2 3] , a = [0:1:3](start:step-size:end) ,
a = linspace(0,3,4)(start, end, count)
Column vector : a = [0;1;2;3]
M N matrix : e.g.) 3 2 matrix => A = [1 3;9 2;5 4]
Empty matrix : A = []
Zero matrix : A = zeros(m,n) e.g.) => A = zeros(2,2)
Ones matrix : A = ones(m,n) e.g.) => A = ones(2,2)
Identity matrix : A = eye(m,n) e.g.) => A = eye(2,2)
7
Basic Operations in MATLAB
Scalar : Using basic operators: addition (+), subtraction (-), multiplication (*), division (/), exponentiation (^)
Vector : Addition and subtraction operate similarly to scalar operations.
However, multiplication and division consider the dimensions of matrices (m×n multiplied by n×m).
Element-wise operations like multiplication and division of arrays use a dot (.) before the operator for array operations.
e.g.) , x ranges from 0 to 3 with 10 numbers
• x = linspace(0,3,10);
• y = x.^2 (y = x^2 => error!)
Inverse matrix : inv(A)
Transpose matrix : transpose(A) or A ’
Determinant : det(A)
Eigenvector, eigenvalue : [X, lamda] = eig(A)
Reference) Inner product : sum(A.*B) or dot(A,B) / Outer product : cross(A,B)
8
For Loop
• Assigning values to matrices • Results
for j=1:5
for i=1:3
a(i, j) = i + j ;
end
end
• A for loop that decreases values
for v = 1.0:-0.2:0.0
disp(v)
end
• A for loop based on specified conditions
for v = [1 5 8 17]
disp(v)
end
9
If Loop
a = zeros(3); b = zeros(3);
for j=1:3
for i=1:3
a(i,j) = rand; % use rand to generate a random number
if a(i,j) > 0.5
b(i,j) = 1;
end
end
end
• Organizing with vectors/matrix makes it easier to do the same thing!
A = rand(3); % A is a 3x3 random number double array
B = zeros(3); % Initialize B as a 3x3 array of zeroes
B(A > 0.5) = 1; % set to 1 all elements of B for which A > 0.5
10
질문 : amabile99@kaist.ac.kr 손지현
11