brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 12. 2020

앤드류 응의 머신러닝 정리 (5-1):옥타브 기본 기능

   온라인 강의 플랫폼 코세라의 창립자인 앤드류 응 (Andrew Ng) 교수는 인공지능 업계의 거장입니다. 그가 스탠퍼드 대학에서 머신 러닝 입문자에게 한 강의를 그대로 코세라 온라인 강의 (Coursera.org)에서 무료로 배울 수 있습니다. 이 강의는 머신러닝 입문자들의 필수코스입니다. 인공지능과 머신러닝을 혼자 공부하면서 자연스럽게 만나게 되는 강의입니다. 


Octave / Matlab Tutorial 

옥타브 / 매트랩 튜토리얼 


Basic Operation (기본 기능)


   You now know a bunch about machine learning. In this video, I like to teach you a programing language, Octave, in which you'll be able to very quickly implement the the learning algorithms we've seen already, and the learning algorithms we'll see later in this course. In the past, I've tried to teach machine learning using a large variety of different programming languages including C++ Java, Python, NumPy, and also Octave, and what I found was that students were able to learn the most productively learn the most quickly and prototype your algorithms most quickly using a relatively high level language like octave. 


   여러분은 머신 러닝에 대해 많은 것을 배웠습니다. 이번 강의에서 프로그래밍 언어인 옥타브를 설명합니다.  옥타브는 학습 알고리즘을 매우 빠르게 구현할 수 있는 언어입니다. 과거에 C++, 자바, 파이썬, Numpy와 같은 다른 많은 프로그래밍 언어를 활용하여 머신 러닝을 가르쳤습니다. 학생들이 옥타브와 같이 상대적으로 고수준의 언어를 사용할 때 가장 빠르게 알고리즘의 프로토타입을 만들었습니다. 


   In fact, what I often see in Silicon Valley is that if even if you need to build. If you want to build a large scale deployment of a learning algorithm, what people will often do is prototype and the language is Octave. Which is a great prototyping language. So you can sort of get your learning algorithms working quickly. And then only if you need to a very large scale deployment of it. Only then spend your time re-implementing the algorithm to C++ Java or some of the language like that. Because all the lessons we've learned is that a time or develop a time. That is your time. The machine learning's time is incredibly valuable. And if you can get your learning algorithms to work more quickly in Octave. 

Then overall you have a huge time savings by first developing the algorithms in Octave, and then implementing and maybe C++ Java, only after we have the ideas working. 


   사실, 실리콘 벨리에서 대규모 학습  알고리즘을 구축할 때 프로토타입을 먼저 만듭니다. 옥타브는 프로토타입을 만들 때 훌륭한 언어입니다. 옥타브로 학습 알고리즘을 빠르게 구현해 보고 필요한 경우에 C++ 또는 자바와 같은 언어로 학습 알고리즘을 재구현합니다. 옥타브가 아닌 다른 언어를 사용할 때 프로그래밍을 하거나 코딩을 하는 시간이 너무 오래 걸립니다. 여러분이 옥타브 프로그램으로 학습 알고리즘을 더 빨리 제대로 구현할 수 있다면, 전체적으로 많은 시간을 아낄 수 있습니다.  그리고, 아이디어가 제대로 작동한다는 확신이 있을 때 C++ 또는 자바로 구현하는 것이 좋습니다.   


   The most common prototyping language I see people use for machine learning are: Octave, MATLAB, Python, NumPy, and R. Octave is nice because open sourced. And MATLAB works well too, but it is expensive for to many people. But if you have access to a copy of MATLAB. You can also use MATLAB with this class. If you know Python, NumPy, or if you know R. I do see some people use it. But, what I see is that people usually end up developing somewhat more slowly, and you know, these languages. Because the Python, NumPy syntax is just slightly clunkier than the Octave syntax. And so because of that, and because we are releasing starter code in Octave. I strongly recommend that you not try to do the following exercises in this class in NumPy and R. But that I do recommend that you instead do the programming exercises for this class in octave instead.


   가장 일반적인 프로토타입 언어는 옥타브(Octave), 매트랩(MATLAB), 파이썬(Python), 넘파이(NumPy), 알(R)입니다. 옥타브는 오픈소스이고, 매트랩은 유료입니다. 매트랩 라이선스가 있다면 이 과정에서 사용할 수 있습니다. 파이썬, 넘파이 또는 R을 사용해도 되지만, 개발 속도가 많이 느릴 것입니다. 파이썬과 넘파이와 같은 언어들은 옥타브보다 문법이 투박하기 때문입니다. 수업 과제를 넘파이와 R을 사용하지 않는 것을 강력히 추천합니다. 이 수업의 프로그래밍 연습은 옥타브가 가장 최적입니다. 


   What I'm going to do in this video is go through a list of commands very, very quickly, and its goal is to quickly show you the range of commands and the range of things you can do in Octave. The course website will have a transcript of everything I do, and so after watching this video you can refer to the transcript posted on the course website when you want find a command. Concretely, what I recommend you do is first watch the tutorial videos. And after watching to the end, then install Octave on your computer. And finally, it goes to the course website, download the transcripts of the things you see in the session, and type in whatever commands seem interesting to you into Octave, so that it's running on your own computer, so you can see it run for yourself. 


   이번 강의에서 옥타브 명령어들을 빠르게 훑어볼 것입니다. 여러 명령어들과 옥타브에서 무엇을 할 수 있는 지를 보여줄 것입니다. 강의 중에 제가 하는 모든 것들은 스크립트가 있습니다. 강의 후에 명령어를 확인하고 싶다면 스크립트를 참고하시기 바랍니다. 처음에는 강의를 끝까지 보시고 옥타브를 설치합니다.  끝으로, 강의 사이트에서 세션에서 본 스크립트를 다운로드하고 명령어들을 실행해 보면 어떤 명령인지를 알 수 있습니다. 



   And with that let's get started. Here's my Windows desktop, and I'm going to start up Octave. And I'm now in Octave. And that's my Octave prompt. Let me first show the elementary operations you can do in Octave. So you type in 5 + 6. That gives you the answer of 11. 3 - 2.5 x 8, 1/2, 2^6 is 64. So those are the elementary math operations. 


   그럼 시작하겠습니다. 여기 윈도 데스크톱이 있고 Octave를 실행합니다. 옥타브 창이 있습니다. 기초적인 연산부터 보여드리겠습니다. 


      5 + 6       %  덧셈      ans = 11

      3 - 2        % 뺄셈       ans = 1     

      5 * 8        % 곱셈       ans = 40

      1/2          % 나눗셈    ans = 0.5

       2^6          % 승          ans = 64 


   이것은 기초적인 수학 연산입니다. '%' 명령어는 주석을 의미합니다.



   You can also do logical operations. So one equals two. This evaluates to false. The percent command here means a comment. So, one equals two, evaluates to false. Which is represents by zero. One not equals to two. This is true. So that returns one. Note that a not equal sign is this tilde equals symbol. And not bang equals. Which is what some other programming languages use. Lets see logical operations one and zero use a double ampersand sign to the logical AND. And that evaluates false. One or zero is the OR operation. And that evaluates to true. And I can XOR one and zero, and that evaluates to one. 


   다음은 논리 연산입니다. '%' 명령어는 주석을 의미합니다.


    1 == 2     % 논리적으로 같다(==)            거짓이므로  ans = 0

    1 ~= 2     % 논리적으로 같지 않다(~=)     참이므로      ans = 1


    여기서, 같지 않다는 부호는 물결과 등호(~=)로 표시합니다. 다른 프로그래밍 언어에서 같지 않다는 부호는 느낌표와 등호 (!=)입니다. 옥타브 프로그램에서는 다릅니다. 


   1과 0이 있는 논리 연산을 살펴보겠습니다. 논리 연산 AND는 두 개의 앤드 기호(&&)를 사용합니다. '. 


   1 && 0       % AND 연산(&&)      거짓이므로 ans = 0

   1 || 0         % OR 연산 (||)          참으로       ans = 1 

   xor (1,0)    % XOR 연산              참으로       ans = 1 



   This thing over on the left, this Octave 324.x equals 11, this is the default Octave prompt. 

It shows the, what, the version in Octave and so on. If you don't want that prompt, there's a somewhat cryptic command PF quote, greater than, greater than and so on, that you can use to change the prompt. And I guess this quote a string in the middle. Your quote, greater than, greater than, space. That's what I prefer my Octave prompt to look like. So if I hit enter. Oops, excuse me. Like so. PS1 like so. Now my Octave prompt has changed to the greater than, greater than sign.Which, you know, looks quite a bit better. 


   왼쪽에 있는 'Octave: 11>' 은 옥타브의 기본 프롬프트입니다. Octave 버전과 기타 정보를 보여줍니다. 


   PS1 (' >> ')        % 프롬프트를 >> 로 변경 


   PS1 명령어는 중간에 있는 문자열 '>> '을 가져옵니다. 저는 이런 Octave 프롬프트를 더 좋아합니다. 이렇게 PS1으로 제 Octave 프롬프트가 부등호 부등호 기호로 바뀌었습니다. 조금 더 나아 보입니다. 



   Next let's talk about Octave variables. I can take the variable A and assign it to 3. And hit enter. And now A is equal to 3. You want to assign a variable, but you don't want to print out the result. If you put a semicolon, the semicolon suppresses the print output. So to do that, enter, it doesn't print anything. Whereas A equals 3. mix it, print it out, where A equals, 3 semicolon doesn't print anything. I can do string assignment. B equals hi Now if I just enter B it prints out the variable B. So B is the string hi C equals 3 greater than colon 1. So, now C evaluates the true.


   다음으로, Octave 변수에 대해 설명합니다.  세미콜론(;)은 결과를 출력하지 않습니다. 


   a = 3;          % 변수 a를 선언하고 3을 할당, 결과를 출력하지 않음

    a = 3          % 변수 a를 선언하고 3을 할당, 결과를 출력

    b = 'hi'      % 변수 b를 선언하고 문자열 'hi'를 입력 

   c = (3>=1)   % 변수 c를 선언하고 3 >= 1 이 참이므로 1을 변수 c에 할당


   결과를 출력하고 싶지 않을 때 세미콜론(;)을 코드 마지막에 넣습니다.  결과를 보고 싶을 때는 세미콜론(;)을 마지막에 넣지 않습니다. 



   If you want to print out or display a variable, here's how you go about it. Let me set A equals Pi. And if I want to print A I can just type A like so, and it will print it out. For more complex printing there is also the DISP command which stands for Display. Display A just prints out A like so. You can also display strings so: DISP, sprintf, two decimals, percent 0.2, F, comma, A. Like so. And this will print out the string. Two decimals, colon, 3.14. This is kind of an old style C syntax. For those of you that have programmed C before, this is essentially the syntax you use to print screen. So the Sprintf generates a string that is less than the 2 decimals, 3.1 plus string. This percent 0.2 F means substitute A into here, showing the two digits after the decimal points. And DISP takes the string DISP generates it by the Sprintf command. Sprintf. The Sprintf command. And DISP actually displays the string. And to show you another example, Sprintf six decimals percent 0.6 F comma A. And, this should print Pi with six decimal places. Finally, I was saying, a like so, looks like this. There are useful shortcuts that type type formats long. It causes strings by default. Be displayed to a lot more decimal places. And format short is a command that restores the default of just printing a small number of digits. Okay, that's how you work with variables.


   변수를 출력하거나 보고 싶을 때 사용하는 함수가 있습니다. 


   a =  pi  % 변수 a를 선언하고 pi 값 3.14 16을 할당


   세미콜론(;)이 뒤에 없으므로 자동으로 a의 값을 모니터에 출력합니다. 다른 출력 명령어가 있습니다. 


   disp(a)    % 변수 a의 값을 출력 

   sprintf(' 2 decimals : %0.2f', a)  % 변수 a를 소수점 아래 2번째 자리까지 출력 

   disp(sprintf(' 2 decimals : %0.2f', a)) % 변수 a의 소수점 아래 2번째 자리까지 출력

   disp(sprintf(' 6 decimals : %0.6f' , a)) % 변수 a를 소수점 아래 6번째 자리까지 출력


   sprintf() 명령어는 지정 문자열과 함께 변수 a의 출력방식을 지정합니다. 출력 값 '2 decimals: 3.14' 이것은 오래된 C 언어 스타일의 문법입니다. C 프로그래밍을 해본 사람들은 출력을 위해 필수적인 문법을 알 것입니다. sprintf() 명령어는 소수점 두 번째 자리까지 나타낸 3.14 문자열을 생성합니다. '%0.2f' 값은 변수 a의 값을 표시할 때 소수점 뒤 두 자리까지 표시한다는 의미입니다. 그리고 disp()는 sprintf 명령어에 의해 생성된 문자열을 받습니다. sprintf () 명령어와 disp()는 실제로 문자열을 출력합니다. 'disp(sprintf(' 6 decimals : %0.6f' , a))' 이것은 Pi를 소수점 아래 6번째 자리까지 출력합니다. 


   format long    % 변수의 자릿수를 길게 표현 

   format short    % 변수의 자릿수를 짧게 표현, 디폴트 값으로 전환 


   이것이 변수를 다루는 방법입니다. 



   Now let's look at vectors and matrices. Let's say I want to assign MAT A to the matrix. Let me show you an example: 1, 2, semicolon, 3, 4, semicolon, 5, 6. This generates a three by two matrix A whose first row is 1, 2. Second row 3, 4. Third row is 5, 6. What the semicolon does is essentially say, go to the next row of the matrix. There are other ways to type this in. Type A 1, 2 semicolon 3, 4, semicolon, 5, 6, like so. And that's another equivalent way of assigning A to be the values of this three by two matrix. 


   이제 백터와 행렬을 보겠습니다.  A에 행렬을 할당합니다. 예제를 보겠습니다. 


   A = [1 2; 3 4; 5 6]    % 3 X 2 행렬 A를 생성하고 값을 할당 


   행렬 A = [1 2; 3 4; 5 6]이라고 선언합니다. 곽 괄호([]) 내의 세미콜론(;)은 행렬의 행을 구분합니다. 행렬을 입력하는 다른 방법이 있습니다.  A = [1, 2; 엔터 3, 4 ; 엔터 5, 6]  계속해서 입력합니다. 이것이 3 X 2 행렬 A를 선언하고 값을 할당하는 방법입니다. 



   Similarly you can assign vectors. So V equals 1, 2, 3. This is actually a row vector. Or this is a 3 by 1 vector. Where that is a fat Y vector, excuse me, not, this is a 1 by 3 matrix, right. Not 3 by 1. If I want to assign this to a column vector, what I would do instead is do v 1;2;3. And this will give me a 3 by 1. There's a 1 by 3 vector. So this will be a column vector. 


   마찬가지로 벡터도 할당할 수 있습니다.


    v = [1, 2, 3]   % 1 X 3차원 행 벡터를 선언하고 값을 할당

     v = [1; 2; 3]   % 3 X 1차원 열 벡터를 선언하고 값을 할당 


   행 벡터는 가로로 긴 벡터이고, 열 벡터는 세로로 긴 벡터입니다.  



   Here's some more useful notation. V equals 1: 0.1: 2. What this does is it sets V to the bunch of elements that start from 1. And increments and steps of 0.1 until you get up to 2. So if I do this, V is going to be this, you know, row vector. This is what one by eleven matrix really.  That's 1, 1.1, 1.2, 1.3 and so on until we get up to two. Now, and I can also set V equals one colon six, and that sets V to be these numbers. 1 through 6, okay. 


   벡터를 만드는 좀 더 유용한 방법이 있습니다.  


   v = 1: 0.1: 2  % 벡터 v를 1부터 2까지 0.1씩 증가하는 성분을 할당 

    v = 1:6          % 벡터 v를 1부터 6까지 1씩 증가하는 성분을 할당 


    'v = 1: 0.1: 2'로 생성되는 1 X 11차원 행렬 v = [1.0, 1.1, 1.2, 1.3,... 2.0]입니다. 1부터 2가 될 때까지 0.1씩 계속 증가합니다. 



   Now here are some other ways to generate matrices. Ones 2.3 is a command that generates a matrix that is a two by three matrix that is the matrix of all ones. So if I set that c2 times ones two by three this generates a two by three matrix that is all two's. You can think of this as a shorter way of writing this and c2,2,2's and you can call them 2,2,2, which would also give you the same result. 


   행렬을 만드는 다른 방법도  있습니다. 


   ones(2,3)                % 모든 성분이 1인 2 X 3 행렬을 생성

   c = 2 * ones (2,3)    % 모든 성분이 2인 2 X 3 행렬을 생성

   c = [2, 2, 2; 2, 2, 2]  % 직접 모든 성분이 2인 2 X 3 행렬을 생성 


   '2 * ones (2,3)'과 'c = [2, 2, 2; 2, 2, 2]'은 같은 결과를 반환합니다. 



   Let's say W equals one's, one by three, so this is going to be a row vector or a row of three one's and similarly you can also say w equals zeroes, one by three, and this generates a matrix. A one by three matrix of all zeros. 


  행렬을 만드는 다른 방법입니다.


   w = ones(1,3)     % 모든 성분이 1인 1 X 3 행 백터를 생성

   w = zeroes(1,3)  % 모든 성분이 0인 1 X 3 행 벡터를 생성  



   Just a couple more ways to generate matrices. If I do W equals Rand one by three, this gives me a one by three matrix of all random numbers. If I do Rand three by three. This gives me a three by three matrix of all random numbers drawn from the uniform distribution between zero and one. So every time I do this, I get a different set of random numbers drawn uniformly between zero and one. 


   다음은 랜덤 성분을 가진 행렬을 생성하는 방법입니다.  


   w = rand(1,3)   % 모든 행렬 성분이 랜덤 숫자인 1 X 3 행렬을 생성

    w = rand(3,3)   % 모든 행렬 성분이 랜덤 숫자인 3 x 3 행렬을 생성 


   rand() 명령어는 0과 1 사이의 균등 분포에서 가져온 랜덤 숫자를 배열합 행렬을 반환합니다. 



   For those of you that know what a Gaussian random variable is or for those of you that know what a normal random variable is, you can also set W equals Rand N, one by three. And so these are going to be three values drawn from a Gaussian distribution with mean zero and variance or standard deviation equal to one.


   randn() 명령어는 가우스 확률 분포 또는 정규 확률 분포의 의한 값을 성분으로 하는 행렬을 반환합니다. 평균이 0이고 분산 또는 표준 편차가 1인 값입니다.  


   w = randn(1,3)    % 모든 행렬 성분이 가우스 확률 분포를 따르는 1 X 3 벡터를 생성  

 


   And you can set more complex things like W equals minus six, plus the square root ten, times, lets say Rand N, one by ten thousand. And I'm going to put a semicolon at the end because I don't really want this printed out. This is going to be a what? Well, it's going to be a vector of, with a hundred thousand, excuse me, ten thousand elements. So, well, actually, you know what? Let's print it out. So this will generate a matrix like this. Right? With 10,000 elements. So that's what W is. 


   더 복잡한 행렬들을 생성할 수 있습니다. 


   w = -6 + sqrt(10) * randn(1, 10000) ;  % 모든 성분이 랜덤 숫자로 1 X 10,000 행렬 생성


   이 명령어는 복잡한 값의 성분을 가진 행렬을 반환합니다. 세미콜론(;)은 을 추가합니다.  sqrt(10) 명령어는 10의 제곱근을 반환합니다. 어떻게 될까요? 이것은 만 개의 행렬 성분으로 이루어진 벡터입니다. 그래서 출력해 봅시다. 이와 같은 행렬을 생성합니다. 10,000개의 행렬 성분을 가진 w입니다. 



   And if I now plot a histogram of w with a hist command, I can now. And Octave's print hist 

command, you know, takes a couple seconds to bring this up, but this is a histogram of my random variable for W. There was minus 6 plus zero ten times this Gaussian random variable. 


   데이터를 막대그래프로 도식화하는 명령어가 있습니다. 


    hist(w)     % w에 대한 막대그래프를 생성 


   hist(w) 명령어로 히스토그램(histogram)인 막대그래프를 그릴 때 몇 초의 시간이 걸립니다. 이것은 'w = -6 + √ (10) * 가우스 확률 변수'에 대한 막대그래프입니다. 



   And I can plot a histogram with more buckets, with more bins, with say, 50 bins. And this is my histogram of a Gaussian with mean minus 6. Because I have a minus 6 there plus square root 10 times this. So the variance of this Gaussian random variable is 10 on the standard deviation is square root of 10, which is about what? Three point one. 


   'hist(w,50)'      % w에 대한 막대그래프를 생성, 막대를 50개로 늘림


   이것이 평균이 -6인 가우스 변수의 막대그래프입니다. 'w = -6 + √ (10) * 가우스 확률 변수'입니다. 평균이 0 인 가우스 확률 분포에 -6을 더했고, √ (10)을 곱했기 때문입니다. 그래서 가우스 확률 변수의 분산은 10이고 표준 편차는 √ (10)입니다. √ (10) = 3.1입니다. 



   Finally, one special command for generator matrix, which is the I command. So I stands for this is maybe a pun on the word identity. It's server set eye 4. This is the 4 by 4 identity matrix. So I equals eye 4. This gives me a 4 by 4 identity matrix. And I equals eye 5, eye 6. That gives me a 6 by 6 identity matrix, i3 is the 3 by 3 identity matrix. 


   마지막으로 행렬을 생성하는 특별한 명령어 eye()입니다.


   I = eye(4)    % 4 X 4 항등 행렬을 생성

   I = eye(5)    % 5 X 5 항등 행렬을 생성

   I = eye(6)    % 6 X 6 항등 행렬을 생성 

   I = eye(3)    % 3 X 3 항등 행렬을 생성


   eye() 명령어는 단어 정체성에 대한 말장난입니다.  



   Lastly, to wrap up this video, there's one more useful command. Which is the help command. So you can type help i and this brings up the help function for the identity matrix. Hit Q to quit. And you can also type help rand. Brings up documentation for the rand or the random number generation function. Or even help help, which shows you, you know help on the help function. So, those are the basic operations in Octave. And with this you should be able to generate a few matrices, multiply, add things. And use the basic operations in Octave. 


   마지막으로 강의를 마무리하기 전에 유용한 명령어 help 명령어가 있습니다.  


   help eye    % 항등 행렬의 생성에 대한 도움말 제공

   help rand   % 랜덤 값을 가진 행렬 생성에 대한 도움말 제공


   help의 결과를 보다가 종료할 때 키보드에서 'q' 버튼을 누릅니다.  심지어 'help help'도 가능합니다. 도움말에 대한 도움말을 제공합니다. 지금까지 옥타브의 기본 연산입니다. 지금까지 배운 것으로 행렬을 만들고 곱하고 더할 수 있습니다. 옥타브에서 기본 연산을 익혀야 합니다.  


   In the next video, I'd like to start talking about more sophisticated commands and how to use data around and start to process data in Octave.


   다음 강의에서 더 복잡한 옥타브 명령어와 데이터를 사용하는 방법을 설명할 것입니다. 옥타브에서 데이터 처리를 시작할 것입니다. 

 


앤드류 응의 머신러닝 동영상 



정리하며 - 옥타브의 기본 기능

   옥타브는 학습 알고리즘을 매우 빠르게 구현할 수 있는 언어입니다. 실리콘 밸리에서 옥타브는 알고리즘이 제대로 동작하는 지를 확인하는 프로토타입을 만들 때 사용합니다. 제대로 작동되는 것이 확인되면 알고리즘을 최적화하기 위해 C++이나 자바와 같은 언어로 활용합니다.  


   옥타브의 기본 기능은 다음과 같습니다.  


1) 사칙 연산

     덧셈 : 3 + 2     % ans 6

     뺄셈 : 3 - 2      % ans 1

     곱셈 : 5 × 8.   % ans 40

     나눗셈: 1 / 2    % ans 0.5

     제곱 : 2 ^ 6      % ans 64   


2) 논리 연산

      1 ==  2          % ans 0     : 참 인지를 묻는 명령

      1 ~= 2           % ans 1.   : 거짓 인지를 묻는 명령. 

      1 && 0          % ans 0     : AND 연산 ( 두 값이 1이면 참)

      1 || 0             % ans 1     : OR 연산 (두 값 중에 하나만 1이면 참)

      xor(1,0)        % ans 1     : XOR 연산( 두 값이 같으면 0, 다르면 1) 


3) 프롬프트 모양 바꾸기

      PS1(' >>')         %  프롬프트의 모양을 >> 모양으로 변경 


4) 변수의 지정

     a = 3            % ans 3           : 변수 a의 값을 3으로 지정    

      a = 3;           % 출력 없음        : a의 값을 3으로 지정하지만 화면에 결과를 출력하지 않음

     a                   % a = 3            : 변수 a의 값을 반환함 

     b = 'hi'         % hi                 : 변수 b에 문자열을 ' '로 지정

     c = (3 >=1)     %1                  : 변수 c에 계산의 결과가 입력, 여기서는 참이므로 1이 입력

 

5) 변수의 출력

       a = pi            % a = 3.1416.    : 파이의 값을 나타내는 상수 pi의 값을 a에 입력

       disp(a);         % 3.1416         : a 파이의 값을 출력

       format long   %                    :  변수의 소수점 뒷자리를 길게 표시

       format shot    %                   :  변수의 소수점 뒷자리를 짧게 표시

       sprintf('2 decimals: %0.2f', a))  % 2 decimals: 3.14 : a의 값을 표현할 자릿수 지정

        disp(sprintf('6 decimals: %0.6f', a) % 6 decimals:3.141593 : 6자리를 표현


6) 행렬의 생성

         A = [1 2; 3 4; 5 6]                : 3 X 2 행렬 A 생성

        v = [1 ; 2; 3]                        : 3 X 1 벡터 v 생성 

        v = [1, 2, 3]                         : 1 x 3 벡터 v 생성 

          v = 1: 0.1: 2           : 1부터 2까지 0.1씩 증가하는 성분들의 벡터 v 생성 (1 X 11 행 벡터)

          v = 1:6                   : 1부터 6까지 1씩 증가하는 성분들의 벡터 v  생성 (1 X 6 행 벡터)

        A = ones (2,3)        : 행렬 성분이 모두 1 인 2 X 3 행렬 A 생성

        A = 2 * ones(2,4)    : 행렬 성분이 모두 2 인 2 X 4 행렬 A 생성

         A = ones(5)             : 행렬 성분이 모두 1인 5 X 5 정방 행렬 A 생성

          A = zeros(2,3)        : 행렬 성분이 모두 0인 2 X 3 행렬 A 생성

        w = rand(1,3)         : 행렬 성분이 0과 1 사이의 랜덤 숫자인 1 X 3 벡터 w를 생성

          w = randn(1,3)       : 행렬 성분이 가우스 분포에 의한 숫자인 1 X3 벡터 w를 생성

                                         (가우스 분포: 평균 0, 표준편차가 1인 값으로 음수와 양수가 조합)

         I = eye(4)               : 4 X 4 항등 행렬 생성


 7) 그래프 그리기

      w = -6 + sqrt(10) * randn(1, 10000) : 행렬 상분이 랜덤 숫자인 1 X 10,000 행렬 생성

       hist(w)                        : 막대그래프 생성 

      hist(w,50)                   : 50개 막대로 그래프 생성


8) 도움말 기능

      help eye                      : eye 함수에 대한 도움말 제공



매거진의 이전글 앤드류 응의 머신러닝 (4-7):정규 방정식의 비가역성
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari