카테고리
Course Basiccourse-basic
코드와 해설을 함께 읽는 학습 문서
Code Detail
연산자, 행렬 연산자, 행렬 연산 함수
course/basic/demo-02.m
코드를 복사해 Octave에서 바로 실행할 수 있습니다.
66 lines
# filename: demo-02.m
# writer: won sunggyu
# date: 2025-04-22
# language: octave
# description: 연산자, 행렬 연산자, 행렬 연산 함수
#------------------------------------------------------------------------------
# 초기화
#------------------------------------------------------------------------------
run("startup.m");
printf(fmt("{mfilename}\n", "#FF5733"));
#------------------------------------------------------------------------------
# 데이터 준비
#------------------------------------------------------------------------------
x = [1 2 3];
y = [4 5 6];
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
#------------------------------------------------------------------------------
# 데이터 연산
#------------------------------------------------------------------------------
# pointwise multiplication
r_pointwise_add = x + y # (1,3) + (1,3) = (1,3)
r_pointwise_sub = x - y # (1,3) - (1,3) = (1,3)
r_pointwise_mul = x .* y # (1,3) .* (1,3) = (1,3)
r_pointwise_div = x ./ y # (1,3) ./ (1,3) = (1,3)
# implicit expansion applize automatically while operating
r_pointwise_add_w = x + y' # (1,3) + (3,1) = (3,3)
r_pointwise_sub_w = x - y' # (1,3) - (3,1) = (3,3)
# inner product
r_inner1 = x * y' # (1,3) * (3,1) = (1,1)
r_inner2 = dot(x, y)
# cross product: 2D or 3D geometry
r_cross = cross(x, y)
# outer product with (pointwise multiplication)
r_outer_w = x .* y' # (1,3) .* (3,1) = (3,3)
# dyadic product (no good for big size data)
r_dyadic_w = x' .* y # (3,1) .* (1,3) = (3,3)
# matrix operation
m_add = A + B # (2,2) + (2,2) = (2,2)
m_sub = A - B # (2,2) - (2,2) = (2,2)
m_mul = A .* B # (2,2) .* (2,2) = (2,2)
m_div = A ./ B # (2,2) ./ (2,2) = (2,2)
m_mmul = A * B # (2,2) * (2,2) = (2,2)
# matrix solve A * x = B
m_sol1 = A \ B # (2,2) * (2,2) = (2,2)
m_sol2 = inv(A) * B # (2,2) * (2,2) = (2,2)
m_sol3 = linsolve(A, B) # (2,2) * (2,2) = (2,2)
#------------------------------------------------------------------------------
# 그래프 그리기
#------------------------------------------------------------------------------