카테고리
Course Basiccourse-basic
코드와 해설을 함께 읽는 학습 문서
Code Detail
랜덤 함수와 히스토그램
course/basic/demo-07.m
코드를 복사해 Octave에서 바로 실행할 수 있습니다.
78 lines
# filename: demo-07.m
# writer: won sunggyu
# date: 2025-04-22
# language: octave
# description: 랜덤 함수와 히스토그램
#------------------------------------------------------------------------------
# 초기화
#------------------------------------------------------------------------------
run("startup.m");
printf(fmt("{mfilename}\n", "#FF5733"));
#------------------------------------------------------------------------------
# 데이터 준비
#------------------------------------------------------------------------------
dx = 0.1;
nx = 4000;
x = 0:dx:dx*(nx-1);
#------------------------------------------------------------------------------
# 데이터 연산
#------------------------------------------------------------------------------
rand ("seed", 1234)
r_rand = rand(1, nx); # Unitform Random Number (0, 1)
r_randn = randn(1, nx); # Normal Random Number (0, 1)
r_rande = rande(1, nx); # Exponentially Distributed Random Number
r_randp = randp(6, 1, nx); # Poisson Random Number, first param is degree
r_randg = randg(1, 1, nx); # Gamma Random Number, first param is shape
[counts, edges] = hist(r_rand, 40);
[countn, edgen] = hist(r_randn, 40);
[counte, edgee] = hist(r_rande, 40);
[countp, edgep] = hist(r_randp, 40);
[countg, edgeg] = hist(r_randg, 40);
#------------------------------------------------------------------------------
# 그래프 그리기
#------------------------------------------------------------------------------
# 그래프
figured("Size", [720, 960], "Move", [-1280, 0], "Name", mfilename);
ax = subplots(5, 2, "Xlabel", "Index", "Ylabel", "Value", "FontSize", 12);
plot(ax(1, 1), x, r_rand);
plot(ax(2, 1), x, r_randn);
plot(ax(3, 1), x, r_rande);
plot(ax(4, 1), x, r_randp);
plot(ax(5, 1), x, r_randg);
barh(ax(1, 2), edges, counts, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(2, 2), edgen, countn, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(3, 2), edgee, counte, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(4, 2), edgep, countp, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(5, 2), edgeg, countg, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
set(ax(1, 1), "Title", "Uniform Random Number (0, 1)");
set(ax(2, 1), "Title", "Normal Random Number (0, 1)", "Ylim", [-3, 3]);
set(ax(3, 1), "Title", "Exponentially Distributed Random Number");
set(ax(4, 1), "Title", "Poisson Distributed Random Number");
set(ax(5, 1), "Title", "Gamma Distributed Random Number");
set(ax(1, 2), "Xlabel", "", "Ylabel", "");
set(ax(2, 2), "Xlabel", "", "Ylabel", "", "Ylim", [-3, 3]);
set(ax(3, 2), "Xlabel", "", "Ylabel", "");
set(ax(4, 2), "Xlabel", "", "Ylabel", "");
set(ax(5, 2), "Xlabel", "", "Ylabel", "");
set_xticknum(ax(1, 2), 3);
set_xticknum(ax(2, 2), 3);
set_xticknum(ax(3, 2), 3);
set_xticknum(ax(4, 2), 3);
set_xticknum(ax(5, 2), 3);
set_axes_layout(ax, [1, 1, 1, 1, 1], [6, 1]);