달력

3

« 2024/3 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

'삽질예방/Matlab'에 해당되는 글 5

  1. 2009.10.10 find, unique, cumsum
  2. 2009.07.01 Profiler
  3. 2009.07.01 inline함수, 무명함수, 함수포인터
  4. 2009.07.01 matlab 구조체
  5. 2009.07.01 셀배열
2009. 10. 10. 08:13

find, unique, cumsum 삽질예방/Matlab2009. 10. 10. 08:13

>> A = magic(3)
A =
     8     1     6
     3     5     7
     4     9     2

>> find(A>5)
ans =
     1
     6
     7
     8

>> [I,J] = find(A>5)
I =
     1
     3
     1
     2
J =
     1
     2
     3
     3

>> [I,J] = find(A==3)
I =
     2
J =
     1


>> A = [1 1 1; 2 2 2; 1 1 1];
>> unique(A,'rows')
ans =
     1     1     1
     2     2     2

>> unique(A)
ans =
     1
     2

find "help cumsum". (maybe useful when converting multinomial distru to cumulative one.
:
Posted by Kwang-sung Jun
2009. 7. 1. 19:32

Profiler 삽질예방/Matlab2009. 7. 1. 19:32

프로파일링
profile on

for i=1:100
    out = myfunc(in);
end

profile viewer

함수 호출 횟수와 수행 시간 등을 보여준다.
테스팅에 편리할듯..
:
Posted by Kwang-sung Jun

inline 함수( 단점: 수식 안에 파라미터가 아닌 변수 값을 넣을 수 없다)
그리고 feval의 사용법.

>> il_humps = inline('1./((x-.3).^2+.01) + 1./((x-.9).^ + .04) - 6','x');
>> il_humps
il_humps =
     Inline function:
     il_humps(x) = 1./((x-.3).^2+.01) + 1./((x-.9).^ + .04) - 6

>> il_humps(1)
ans =
   -2.9035

>> y = feval('sin', pi*(0:4)/4)
y =
         0    0.7071    1.0000    0.7071    0.0000

다음은 무명함수와 함수포인터를 사용하는 예제이다.

function tmpfunc()
    clc;
   
    a = 3;
 
    % impossible
    % myfunci = inline('sin(x)+a', 'x');
   
    % 무명함수를 정의하고 그것의 포인터를 넘긴다.
    myfunc = @(x) sin(x)+a;
   
    myfunc3 = @(x, y) x+y; % 인자가 두개인 경우의 예제
    myfunc3(3, 4)
   
    myeval_pointer = @myeval;  %% function pointer.
   
    a=0; % this won't change the function of 'myfunc'
   
    % 아래 함수 세개의 결과는 같다
    fprintf('myeval=%f\n', myeval(myfunc, 3));
    fprintf('feval=%f\n', feval(myfunc, 3));
    fprintf('myeval_pointer=%f\n', myeval_pointer(myfunc, 3));
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % 함수 포인터를 이용하여 이것을 배열에 넣고 사용하기! 매우 편리하겠지???

    myfunc2 = @(x) sin(x)+a; % now a is 0
    funary = {myfunc, myfunc2};
    funary{1}(3)
    funary{2}(3)
end

function ret = myeval(func, x)
    ret = func(x);
end


:
Posted by Kwang-sung Jun
2009. 7. 1. 19:17

matlab 구조체 삽질예방/Matlab2009. 7. 1. 19:17

구조체는 다음과 같이 쓴다. 원소를 더 추가하고 싶으면
circle(2).어쩌구.. 라고 쓰면 새로이 멤버가 추가된다.
>> circle.radius = 3.4;
>> circle.color='green';
>> circle.linestyle=':';
>> circle.center = [2.3 -1.2];
>> circle

circle =

1x1 struct array with fields:
    radius
    center
    color
    linestyle


다음과 같이 일괄적으로 구조체를 생성하는 방법도 있다.
>> values1 = {2.5 'sqrt(2)' 25.4};
>> values2 = {[0 1] [2.3 -1.2] [-1 0]};
>> values3 = {'--' ':' '-.'};
>> values4 = {'red', 'green', 'blue'};
>> values5 = {'yes', 'no', 'yes'};

circle = struct('radius', values1, 'center', values2, ...
'linestyle', values3, 'color', values4, 'filled',values5);


현재 circle구조체는 멤버가 셋이며 이 경우 다음과 같은 표현을 쓸 수 있다. 스트링을 이용하여 구조체의 멤버 접근을 일반적으로 할 수 있다는 것은 언젠가 편하게 쓰일 것 같다.

>> fldstr = 'color';
>> circle.(fldstr)

ans =
red

ans =
green

ans =
blue

fieldnames(circle) % 모든 필드 이름의 리스트를 리턴한다.


:
Posted by Kwang-sung Jun
2009. 7. 1. 19:12

셀배열 삽질예방/Matlab2009. 7. 1. 19:12

문자열은 배열 그대로 쓰면 공백들이 들어가서 불편하고, 다음과 같이 셀 배열을 이용한다.

>> strary(1) = {'abc'};
>> strary(2) = {'안녕친구들'};
>> strary
strary =
    'abc'    '안녕친구들'

>> strary(1)
ans =
    'abc'

>> strary{1}
ans =
abc

다음은 2행 3열의 셀 배열을 생성해 준다.

c = cell(2,3);


대부분 값의 대입과 값의 이용시에는
c{1,1} 와 같이 접근하면 된다.
그냥 c(1,1)이라고 하면 데이터 타입이 뭔지만 알려주는 역할을 하게 되고 값은 알려주지 않는다.

p.s.
"경우에 민감한(case-sensitive) 필드를 ...라 한다."....... 번역..... 진짜 한심한 수준의 번역이다..
:
Posted by Kwang-sung Jun