[Matlab] practice_2

dewdew·2023년 6월 28일
0

Matlab & Simulink

목록 보기
2/6
post-thumbnail

[Matlab] 2일차 학습

학습 소스 링크 첨부 (06-10)

% 2d 를 1d 로(column major)
A = rand(3,4)
A(:)
% row major 는 "Transposed" 를 사용
AA = A'
AA(:)
ans'

% 1d 를 2d 로
% 1. 4x3 in colum major
a = ans'
A = zeros(4,3)
A(:) = a
% 2. 3x4 in row major
B = zeros(3,4)
B(:) = a
B = B'
A= rand(4,6)
A(:, 2) = []
A(1, 2) = 0
A
whos A

% 3d array
A(:, :, 2) = rand(4,5)
whos A
% operations with nonmatching dimensions 
% matlab 은 scalar 와 vector 를 자동으로 matching 시켜주는구나
a = rand(6,5)
a + 3
a(1:3, 2:4) = 0.7
a(:,:) = 1
% comparision
a = rand(10,3)
a < 0.5

% 1-9 까지의 숫자로 어떤 직선을 그려도 합이 동일한 배열로
C = magic(3)
1./C
C.^2 
% 동일한 dimension 이 와줘야 하는게 원칙, but scalar is specially treated!!

%complex number
x1 = 3 + 5i
x2 = 1 - 0.741j
x1 / x2
real(x1)
imag(x1)
M = abs(x1)
whos x1
phi = angle(x1) * (180/pi)

x = M * exp(1j*phi)

% 3x1 의 complex array
x = rand(3,1) + 1j*rand(3,1) 
whos x

x = x' % 허수부의 부호가 바뀜(conjugate)
x'

% do not use arctan
atan(-1/1) * (180/pi) % -45
atan(1/-1) * (180/pi) % 135 or -225
% 4사분면을 다 쓰는 atan2 를 사용하자
atan2(-1, 1) * (180/pi)
atan2(1, -1) * (180/pi)

%string
s1 = 'I am'
s2 = ' a girl'
whos s1 s2
abs(s1) % ASCII Code
char(s1)
s12 = [s1, s2]
s3 = 'who are you?'
whos s1 s2 s3
s4 = [s1, s2, ' ' ; s3]
s5 = 'dad''s watch'
num2str(pi)
whos ans
num2str(pi, "%.6f") % f : fixed point 
num2str(pi, "%.4e") % e : floating point
i = 35
s = ['answer: ', num2str(i),'km/h']
% ! array is a collection of homogeneous data ! %

% load and save
who
whos
save
clear
who
load
who
save aaa.mat
clear
load aaa.mat
who
a = rand(3,2); b = rand(3,3);
save test.txt a b -ascii
type test.txt
% load test.txt % 오류가 나는 이유 3x2, 3x3 가 하나로 묶여서 array 가 비었기 때문
a = rand(10,3); b = rand(3,3);
save test.txt a b -ascii
type test.txt
load test.txt
test

% directory command
dir
pwd
which pwd
which who
path

% linear system
A = [3,1,2;2,2,1;1,-3,2]
b = [1;-3;0]
x = inv(A) * b
A * x - b

x = A \ b % 역행렬을 구하지 않고 LU 분해로 계산
% x = b / A (X)

% solution of Ax =  b
% 식 < 미지수 : 무한히 많은 해 ===> 2 norm minimum
A = rand(3,5)
b = rand(3,1)
x = A \ b
A*x-b

% 식 > 미지수 : 해가 존재하지 않음 ===> least square problem
A = rand(6,3)
b = rand(6,1)
x = A \ b

% logic 
2 < 5
2 > 5
x = 2
x == 3 | x > 0
x == 3 & x > 0
x = 0.5
0 < x < 1 % false 인 이유? 0 < 0.5 = 1 & 1 < 1 (logic 1) => 두개로 나누자
0 < x & x < 1 
-100 & 2000 % 0 이 아닌 모든 수는 logic '1'

% logical indexing
x = rand(10,1)
x(4)
a = rand(8,1)
i = ones(4, 1)
j = logical(i)
a(i)
whos j
a(j)
a > 0.5
a(ans)
a( a > 0.5 )
% 2d logical array
A = rand(8,3)
A > 0.5
A .* ( A > 0.5 )

% cell array == collection of heterogeneous data
%a{1,1} = 'John Smith'
clear a
a{1,1} = 'John Smith' % 1x10 char array element 가 vector
a(1,1) = {'John Smith'}
a(1,2) = {67.25}
a{2,1} = rand(2,1) > 0.5 % == a(2,1) = {rand(2,1) > 0.5}
a{2,2} = rand(5, 5)
a{2,2}
a{2,2}(4,1)
whos a
% cellplot(a)

% structure array : use field(string) instead of subscript(number)
% guest.name = 'A Kim'
% guest.room = 306
% guest.data = rand(3,2)
% whos guest
% guest.data
% guest.data(3,2)
guest(2).discount = 0
guest(2).name = 'park'
guest(3).room = 708
guest
whos guest

% built-in function

% 1. function for scalar
theta = rand(10,5)
sin(theta)
fix(3.6)
floor(3.6)
fix(-3.6) % 0 을 향해 (-3)
floor(-3.6) % 마이너스 무한대를 향해 (-4)

% 2. function for vector
x = rand(1000,1)
max(x)
min(x)
sum(x)
mean(x)
sort(x)
theta = rand(10,4)
max(theta) % 각각의 column 의 최대값 -> 결과가 row vector
max(ans)
max(max(theta))

% 3. function for matrix
A = rand(3)
eig(A) % 고유값, scalar 값 찾기
% help eig
[V, D] = eig(A) % V : complex vector / D ==> eigen value A*V = D*V
v1 = V(:, 1)
d1 = D(1, 1)
A * v1 - d1 * v1
v2 = V(:, 2)
d2 = D(2, 2)
A * v2 - d2 * v2

% 4. function with logical output
isreal(v2)
~isreal(v2)
exist('A')
exist('AAA')
isequal(2,3) % vector 를 비교하고싶을 때
2 == 3

% [1 2] == [1 3 2] error
isequal([1 2], [1 3 2])






profile
연습장

0개의 댓글