#title Matlab 강의 Day2 Matlab data type * Hierarchy of data object (개체의 계층 구조?) * Logical array * Character array * Double array * Multi-dimensional array * Cell array * Structure array * Function Handle array ---- [[TableOfContents]] == Logical array == 0 아니면 1. True or False '''예제 1.''' 마방진에서 5보다 큰 숫자를 찾아낸다. {{{#!plain matlab >> A = magic(3) A = 8 1 6 3 5 7 4 9 2 >> I = A > 5 I = 1 0 1 0 0 1 0 1 0 >> whos Name Size Bytes Class Attributes A 3x3 72 double I 3x3 9 logical }}} 여기서 I 값의 1은 True, 0은 False를 나타낸다. '''예제 2.''' 마방진에서 5보다 큰 숫자에 100값을 넣는다. {{{#!plain matlab %% find 함수 사용 A = magic(3) A(find(A>5)) = 100 %% A 내부의 값이 5보다 큰 경우에 100으로 변경 A = magic(3) A(A>5) = 100 }}} {{{#!plain matlab A = 8 1 6 3 5 7 4 9 2 A = 100 1 100 3 5 100 4 100 2 }}} '''예제 3.''' 다음 3식을 한 줄의 명령어 합친 뒤 plot y = sin(x), 00 & x<=1) + exp(-x).*(x>1 & x<=2) + 3*(x>2) plot(x,y) }}} attachment:Logicalarrayex3.png == Character array (String) == * Numeric Array와 인덱싱이 동일 {{{#!plain matlab str1 = 'what''s your name?' str2 = 'My name is xxx' str4 = [srt1 ' ' str2] char(str1, str2) }}} == Double array == {{{#!plain matlab >> pi ans = 3.1416 >> format long >> pi ans = 3.141592653589793 >> 2*12.3 ans = 24.600000000000001 >> format >> pi ans = 3.1416 >> single(pi) ans = 3.1416 >> format long >> single(pi) ans = 3.1415927 }}} 미국 달러 기준의 format {{{#!plain matlab >> format bank >> 1 ans = 1.00 }}} 분수로 수를 표현하는 format {{{#!plain matlab >> format rat >> pi ans = 355/113 }}} == Multi-dimensional array (3D array) == {{{#!plain matlab >> A = magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> A(:,:,2)=pascal(4) A(:,:,1) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 A(:,:,2) = 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20 >> A(:,1,2) ans = 1 1 1 1 >> A(2,2,2) ans = 2 }}} {{{#!plain matlab >> format compact >> A = cat(3, magic(3), pascal(3)) A(:,:,1) = 8 1 6 3 5 7 4 9 2 A(:,:,2) = 1 1 1 1 2 3 1 3 6 }}} == Cell array == * 여러개의 데이터 타입을 동시에 포함 * 각 셀은 단독 공간(a room)으로 간주한다. {{{#!plain matlab x{1,1} = 'matlab' x{1,2} = magic(4); x{2,1} = @sin; x{2,2} = struct('age', 20, 'sex', 'male'); cellplot(x, 'legend') }}} attachment:MatlabDay2/Cell_arrayex_1.png {{{#!plain matlab >> x{1} ans = matlab >> x{1,1} ans = matlab >> x{1,2} ans = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> x{2,1} ans = @sin >> x{2,2} ans = age: 20 sex: 'male' >> x{1,2}(:,3) ans = 3 10 6 15 }}} {{{#!plain matlab >> whos Name Size Bytes Class Attributes x 2x2 660 cell }}} x{:} == x{1}, x{2},... {{{#!plain matlab >> x x = 'matlab' [4x4 double] @sin [1x1 struct] >> x(:) ans = 'matlab' @sin [4x4 double] [1x1 struct] >> x{:} ans = matlab ans = @sin ans = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ans = age: 20 sex: 'male' }}} x(1,1)과 x{1,1}은 다르다. {{{#!plain matlab >> x(1,2) ans = [4x4 double] >> x{1,2} ans = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 }}} 데이터 밀어내기 {{{#!plain matlab >> x(end,:) = {123 @sin} x = 'matlab' [4x4 double] [ 123] @sin }}} 셀 삭제 {{{#!plain matlab >> x x = 'matlab' [4x4 double] [ 123] @sin >> x(end,:) = [] x = 'matlab' [4x4 double] }}} == Structure array ==