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

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,279评论 0 10
  • 暖风吹得梅香来, 杨柳未折笛不闻。 难得二月莺飞季, 唯惜赏春独一人。
    孤独的北风阅读 216评论 0 2
  • 观点新颖,给人耳目一新的感觉,有一期告诉我们要多读书,给我们讲读书得好处。 不必留恋过去,往前走,顺势而行,明天比...
    蟒蛇king阅读 166评论 0 0
  • 蝴蝶日记小蝶阅读 166评论 0 0