Matlab Basics (1)
Matlab Basics (1)
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];
f = sin(b);
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
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 (^)
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!)
8
For Loop
• Assigning values to matrices • Results
for j=1:5
for i=1:3
a(i, j) = i + j ;
end
end
10
질문 : [email protected] 손지현
11