Lynda_Learning_Octave

写在最前面
下定决心好好整理学习过的Lynda.com课程的笔记,在以前的学习过程中光是跟着视频学习,看似是学的很快,但是没有巩固,很快就忘记了。视频学习的一个弊病是不利于复习,而这恰恰是书本学习的强项,因为可以随心所欲的划重点,复习时可以剔除啰嗦的部分,只看重点。这也是我开始整理学习笔记的初衷,希望能一直坚持下去。

Lynda.com是全英文学习网站,所以所有笔记内容都是英文,而关于自己的心得体会还有思考,以及个别生涩单词,我会用中文在文中予以标注,便于自己反复查看。

0.Introduction

What You Should Know

  • How to install software on your operating system
  • How to get your data into a text file, such as the comma-separated value(CSV) files generated by Excel and other database programs
  • How to work with free software

1.Introducing Octave

Downloading Octave

  • http://www.sourceforge.net
  • search for "octave"
  • For Mac OS X:
    • Go to www.octave.org
    • Find the link Octave Wiki

Running Octave

octave:1> a = 3
a = 3
octave:2> b = a
b = 3
octave:3> PS1 <'>>'>;
>> exit  % close octave

Getting help

>> help help
>> help pi
>> doc % press Ctrl + C to leave DOC

Support/Help: https://www.gnu.org/software/octave/support.html
Online doc: https://octave.org/doc/interpreter/
PDF: https://octave.org/octave.pdf


2.Surveying Basic Octave Commands

01. Using build-in commands

>> pi
ans = 3.1416
>> format long
>> pi
ans = 3.14159265358979
>> format short
>> pi
ans = 3.1416
>> a = pi
a = 3.1416
>> b = pi;
>> disp(a)
  3.1416
>> c = rand()
c = 0.27080
>> disp(c)
  0.27080
>> randn()
ans = 0.50997
>> randn()
ans = 0.31921
>> randn()
ans = -0.34053
>>

02. Assigning value to variables

>> a = 3
a = 3
>> b = a
b = 3
>> a = pi
a = 3.1416
>> s = 'Product Code'
s = Product Code
>> st = 'Product"
error: unterminated string constant
parse error:

    syntax error

 >>> st = 'Product" 
                            ^

>> st = "Product"
st = Product
>> a = rand()
a = 0.41793
>> a = randn()
a = -0.12372
>> 

03. Introducing mathematical operators

  • Math operators in Octave
    • +, addition (e.g. 1 + 3 = 4)
    • -, subtraction (e.g. 3 - 1 = 2)
    • *, multiplication (e.g. 3 * 6 = 18)
    • /, division (e.g. 6 / 3 = 2)
    • ^ or **, exponentiation求幂 (6 ^ 3 = 216)
    • mod(x, y), modular模的 or reminder division (e.g. mod(5, 2) = 1)
  • Logical operators in Octave
    • ==, is equal to (e.g. 5 == 5 is TRUE, 5 == 4 is FALSE)
    • ~= or !=, not equal to (e.g. 4 != 5 is TRUE)
    • &&, and (e.g. TRUE && TRUE is TRUE)
    • ||, or (e.g. TRUE || FALSE is TRUE)
    • xor(x, y), either x or y is TRUE, but not both (e.g. xor(TRUE, TRUE) is FALSE)
>> 3 + 5
ans = 8
>> 5 - 3
ans = 2
>> 5 * 3
ans = 15
>> 5 / 3
ans = 1.6667
>> 5 ^ 3
ans = 125
>> 5 ** 4
ans = 625
>> mod(24, 5)
ans = 4
>> a = 3
a = 3
>> b = 2
b = 2
>> a == b
ans = 0
>> b = 3
b = 3
>> a == b
ans = 1
>> a ~= b
ans = 0
>> true && false
ans = 0
>> true && true
ans = 1
>> 1 && 0
ans = 0
>> 1 && 1
ans = 1
>> 1 || 0
ans = 1
>> 0 || 0
ans = 0
>> 1 || 1
ans = 1
>> xor(1, 0)
ans = 1
>> xor(1, 1)
ans = 0
>>

04. Calculating values using built-in functions and variables

  • Useful built-in functions
    • sqrt(x) finds the square root of x
    • nthroot(x) finds the nth root of x (e.g. nthroot(27,3) returns 3)
    • fix(x) truncates a number and returns just the integer part
    • ceil(x) rounds a number up
    • floor(x) rounds a number down
    • round(x) rounds a number to the closest integer (.5 goes up)
    • max(x) finds the largest number in an input set
    • min(x) finds the smallest number in an input set
    • factorial(x) finds the factorial阶乘 of an input (e.g. 5! = 120)
    • primes(x) returns primes质数 up to x (e.g. primes (8) returns 2, 3, 5, 7)
    • list_primes(x) lists the first x primes
>> sqrt(16)
ans = 4
>> sqrt(15)
ans = 3.8730
>> nthroot(81, 4)
ans = 3
>> fix(3.5)
ans = 3
>> ceil(6.001)
ans = 7
>> floor(6.999)
ans = 6
>> round(4.49)
ans = 4
>> round(4.5)
ans = 5
>> v = [1, 2, 3, 4]
v = 
     1   2   3   4
>> max(v)
ans = 4
>> min(v)
ans = 1
>> factorial(5)
ans = 120
>> primes(23)
ans = 
     2   3   5   7   11   13   17   19   23
>> list_primes(23)
ans = 
  Columns 1 through 15:
     2   3   5   7   11   13   17   19   23   29   31   37   41   43   47
  Columns 16 through 23:
     53   59   61   67   71   73   79   83
>>

05. Manipulating strings

  • String assignments and concatenation一系列相互关联的事物
    Assign a string to a variable by enclosing it in either single or double quotes
    • strcat(s1, s2...) concatenates strings, which can be literals or variables, and removes whitespace at the end of a string
    • cstrcat(s1, s2...) concatenates strings, which can be literals or variables, and keeps whitespace at the end of a string
  • Converting number to strings
    • num2str(x) converts a number to a string
    • num2str(x, p) converts a number to a string, rounded to p digits
  • String comparison
    • strcmp(s1, s2) returns 1 if strings are identical同一的, 0 otherwise
    • strncmp(s1, s2, n) returns 1 if first n characters of strings are identical, 0 otherwise
    • strcmpi(s1, s2) returns 1 if the strings s1 and s2 are the same, ignoring case
    • strncmpi(s1, s2, n) returns 1 if first n characters of strings are identical, 0 otherwise, ignoring case
  • String comparison (Trimming修剪 and Processing Strings)
    • deblank(s) removes trailing拖尾的 whitespace from a string or array
    • strtrim(s) removes leading and trailing whitespace
    • strtrunc(s, n) truncates a string to length n
>> s1 = "Spring "
s1 = Spring
>> s2 = "catalog"
s2 = catalog
>> strcat(s1, s2)
ans = Springcatalog
>> cstrcat(s1, s2)
ans = Spring catalog
>> i = 551
i = 551
>> strcat(s2, i)
warning: range error for conversion to character value
ans = catalog
>> num1 = num2str(i)
num1 = 551
>> strcat(s1, num1)
ans = Spring551
>> strcmp(s1, s2)
ans = 0
>> s3 = deblank(s1)
s3 = Spring
>>

06. Performing conditional steps using if statements

  • Creating a basic if statement
if (condition)
    statement
endif
  • File1: sdata.m
% Created by Percy
% Script name is sdata
      
sales = 500
if (sales >= 500)
    printf ("Sales target reached. \n");

endif
>> sdata
sales = 500
Sales target reached.
>>
  • File2: sdata.m
% Created by Percy
% Script name is sdata
      
sales = 400
if (sales >= 500)
    printf ("Sales target reached. \n");

endif
>> sdata
sales = 400
>>
  • Creating a basic if...else statement
if (condition)
    statements
else
    statements
endif
  • File3: sdata.m
% Created by Percy
% Script name is sdata
      
sales = 400
if (sales >= 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 400
Sales target not reached.
>>
  • File4: sdata.m
% Created by Percy
% Script name is sdata
      
sales = 500
if (sales >= 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 500
Sales target reached.
>>
  • Creating a basic if...elseif...else statement
if (condition)
    statements
elseif
    staements
endif
  • File5: sdata.m
% Created by Percy
% Script name is sdata
      
sales = 500
if (sales >= 750)
    printf ("Bonus target reached. \n");
elseif (sales = 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 500
Sales target reached.
>>
  • File6: sdata.m
% Created by Percy
% Script name is sdata
      
sales = 400
if (sales >= 750)
    printf ("Bonus target reached. \n");
elseif (sales = 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 400
Sales target not reached.
>>
  • File7: sdata.m
% Created by Percy
% Script name is sdata
      
sales = 800
if (sales >= 750)
    printf ("Bonus target reached. \n");
elseif (sales = 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 800
Bonus target reached.
>>

3.Manipulating Matrices

01. Defining vector向量 and matrices矩阵

>> M = [1 2 3; 4 5 6]
M =
      1    2    3
      4    5    6
>> rv = [1 2 3]
rv = 
      1    2    3
>> cv = [1; 2; 3]
cv = 
      1
      2
      3
>>

02. Adding, subtracting, and multiplying matrices

  • Subtraction requires matrices of the same dimensions尺寸规格
  • Multiplication requires matrices where the columns of the first matrix match the rows of the second matrix. The result is a matrix with the rows of the first matrix and the columns of the second.
  • Matrix multiplication is not commutative交换的 - order matters!
  • Adding, subtracting, multiplying, or dividing by a scalar value affects every element of the matrix.
>> M = [1 2 3; 4 5 6]
M = 
      1    2    3
      4    5    6
>> N = [5 6; 7 8; 9 10]
N = 
      5    6
      7    8
      9    10
>> M * N
ans = 
      46    52
     109   124
>> N * M
ans = 
      29    40    51
      39    54    69
      49    68    87
>> M * 4
ans = 
      4    8    12
     16   20    24
>>

03. Generating useful matrices

>> ones(2, 3)
ans = 
      1    1    1
      1    1    1
>> zeros(5, 4) 
ans = 
      0    0    0    0
      0    0    0    0
      0    0    0    0
      0    0    0    0
      0    0    0    0
>> eye(3)
ans = 
Diagonal Matrix
      1    0    0
      0    1    0
      0    0    1
>> M = [1 2 3; 4 5 6; 7 8 9]
M = 
      1    2    3
      4    5    6
      7    8    9
>> M * eye(3)
ans = 
      1    2    3
      4    5    6
      7    8    9    
>> v = [1: 0.1: 2]
v = 
    Columns 1 through 8:
      1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000
    Columns 9 through 11:
      1.8000    1.9000    2.0000
>> W = rand(2, 3)
W = 
      0.89360    0.19536    0.34167
      0.75238    0.21295    0.81458
>> randn(3, 4)
ans = 
     -0.076539   -0.236123   -0.630989    0.119407
     -0.248545   -0.749690   -0.124957    0.616097
      1.002778   -0.255473   -1.626574   -0.173041
>>

04. Transposing转置 and inverting matrices

>> eye(2)
ans = 
Diagonal Matrix
      1    0
      0    1
>> B = [1 2; 3 4]
B = 
      1    2
      3    4
>> inv(B)
ans = 
     -2.00000    1.00000
      1.50000   -0.50000
>> B * inv(B)
ans = 
      1.00000    0.00000
      0.00000    1.00000
>> pinv(B)
ans = 
     -2.00000    1.00000
      1.50000   -0.50000
>> A = [1 2 3; 4 5 6; 7 8 9]
A = 
      1    2    3
      4    5    6
      7    8    9
>>

05. Performing element-wise calculations

  • Multiplying two matrices does not go element by element.
> A = [10 15; 20 25]
A = 
      10    15
      20    25
>> A * 3
ans = 
      30    45
      60    75
>> B = [4 8; 9 18]
B = 
      4    8
      9   18
>> A * B
ans = 
      175    350
      305    610
>> A .* B
ans = 
       40    120
      180    450
>>

06. Referring to matrix rows and columns

>> v = [1: 0.1: 2]
v = 
    Columns 1 through 8:
      1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000
    Columns 9 through 11:
      1.8000    1.9000    2.0000
>> v(7)
ans = 1.6000
>>v(7:9)
ans = 
      1.6000    1.7000    1.8000
>> A = [1 5; 8 9]
A = 
      1    5
      8    9
>> A(2, 1)
ans = 8
>> A(2, :)
ans = 
      8    9
>> A(:, 1)
ans = 
      1
      8
>>

4.Managing Executable Octave Programs

01. Sending output to the screen

  • The following variable assignments and printf() command:
day = 2;
month = "August";
printf ("Signature validated on %s %d.\n Thank you!\n", month, day);
  • Produce this output:
Signature validated on August 2.
Thank you!
  • sprintf() uses the same conventions协议, but sends the output to a sring variable
  • Helpful Codes for printf() and sprintf()
    • %i prints an integer as a decimal number
    • %u prints an integer as an unsigned decimal number
    • %f prints a floating-point number in normal(fixed-point) notation
    • %s prints a string
    • %% prints a "%" character
    • \n prints a line feed character
>> day = 2
day = 2
>> month = "August"
month = August
>> printf ("Signature validated on %s %d.\n Thank you!\n", month, day)
Signature validated on August 2.
 Thank you!
>> s = sprint("%s %d", month, day)
s = August 2
>> disp(s)
August 2
>>

02. Sending output to a file

  • Assign the data to a variable, such as A
  • Type a command such as save datafile.mat A
  • Can also use options
    • -ascii writes the data without header information
    • -append appends data instead of overwriting, as long as you assign the data to a different variable name
    • -mat7-binary is MatLab version 7's binary format
  • To write data to a comma-separated value file, use this command:
    csvwrite("filename", X)
  • File is saved to the directory with the Octave executable
>> a = [1 2; 3 4; 5 6]
a = 
      1    2
      3    4
      5    6
>> save -ascii datafile.mat A
>> csvwrite("csvdata.mat", A)
>>

03. Using data stored in a external file

  • To assign the data to a variable, such as C, type a command such as
    C = load("datafile.mat")
  • To read data from a comma-separated value file, use this command:
    csvread("filename")
  • Octave looks in the directory with the Octave executable
>> C = load("datafile.mat")
C = 
      1    2
      3    4
      5    6
>> D = csvread("csvdata.mat")
D = 
      1    2
      3    4
      5    6
>>

04. Defining a function

  • File: displaypi.m
% create a file named displaypi.m

function displaypi
        disp(pi);
endfunction
  • Output
>> displaypi
  3.1416
>>
  • File: timespi.m
% create a file named timespi.m

function timespi(x)
        disp(x * pi);
endfunction
  • Output
>> timespi(10)
  31.416
>> timespi(8)
  25.133
>>
  • File: cubed.m
% create a file named cubed.m

function retval = cubed(x)
        retval = x ^ 3
endfunction
  • Output
>> a = cubed(5)
retval = 125
a = 125
>>

05. Creating a executable script

  • File: firstscript.m
% Percy, July 1, 2018

a = 3;
disp(a * 3)
  • Output
>> firstscript
  9
>>

06. Adding comments to an Octave program

  • Lines that start with a % or # are comments
  • Can add a % on a line after the command; everything after it is a comment
  • Some good uses for comments are to indicate:
    • The start of a script file so Octave doesn't confuse it for a function file
    • When the file was created and by whom
    • What type of data a file, function, or subroutine子程序 expects
    • Whether the file depends on other files
    • What subroutines do and how they do it
  • File: comments.m
# Created July 1, 2018 by Percy

sales = 800

% $750 is new daily target

if (sales >= 750);
    printf ("Bonus target reached. \n");
elseif (sales = 500);      % Formerly top bonus target amount.
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif

07. Debugging your Octave code

  • File: debugging.m
% Testing if statements

sales = 800;

if (sales >= 500);
    printf ("Bonus target reached. \n");
elseif (sales >= 750);  
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
  • Output
>> debugging
Bonus target reached.
>>
  • File: debugging.m (fixed)
% Testing if statements

sales = 600;

if (sales >= 750);
    printf ("Bonus target reached. \n");
elseif (sales >= 500);  
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
  • Output
>> debugging
Sales target reached.
  • File: debugging.m (pause)
% Testing if statements

sales = 600;

printf ("Press Enter to continue.")
pause;

if (sales >= 750);
    printf ("Bonus target reached. \n");
elseif (sales >= 500);  
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
  • Output
>> debugging
Sales = 600
Press Enter to continue.
Sales target reached.
>>

5.Plotting Data

01. Creating a simple plot

  • Code: plot
>> X = -5:0.1:5
% there will be 101 results
>>plot(X, sin(X))
>>plot(X, cos(X))
>>
  • Output: sin(x)
    sin(x).png
  • Output: cos(x)
    cos(x).png

02. Summarizing data using a histogram柱状图

  • Code: hist
>> X = [1, 3, 7, 14, 15, 16, 14]
X = 
      1    3    7    14    15    16    14
>> hist(X)
>> hist(X, 3)
>>
  • Output: hist(x)
    hist(x).png
  • Output: hist(x, 3)
    hist(x, 3).png

03. Creating a scatter分散 plot

  • Code: scatter
>> X = [1, 3, 5, 7, 9]
X = 
      1    3    5    7    9
>> Y = [15, 4, 18, 3, 9]
Y = 
      15    4    18    3    9
>> scatter(X, Y)
>> scatter(X, Y, "red")
>> scatter(X, Y, "red", "filled")
>>
  • Output: scatter(x, y)
    scatter(x, y).png
  • Output: scatter(x, y, "red")
    scatter(x, y, "red").png
  • Output: scatter(x, y, "red", "filled")
    scatter(x, y, "red", "filled").png

6.Conclusion

Further Resources

  • sourceforge.net and search for Octave
  • The Manga Guide to Linear Algebra, from No Starch Press
  • Data Science for Business, from O'Reilly Media

你可能感兴趣的:(Lynda_Learning_Octave)