Octave study notes

Andrew Ng recent study teacher machine learning courses, Octave language courses in programming practices used to record the Commemoration of the learning process, if there are questions, please correct me, welcome the exchange of learning.

1. Basic arithmetic

>> 2 + 3
ans =  5
>> 4 - 8
ans = -4
>> 2 * 4
ans =  8
>> 5 / 2
ans =  2.5000
>> A = [1,1;3,4]
A =
1 1
3 4
>> A > 1
ans =
0 0
1 1

  

2. The basic logic operations

>> 2 == 3
ans = 0
>> 2 ~= 3
ans = 1
>> 2 || 3
ans = 1
>> 0 && 2
ans = 0
>> 3 >= 4
ans = 0
>> 3 < 5
ans = 1

  

3. The basic vector representation? And take the specified element

% 1 row vector (comma or space, division) 
>> V = [1 2. 3] 

V = 
   1 2. 3 
>> V = [l, 2,3] 
V = 
   1 2. 3 
  % of the designated column line vector fetch 
>> V (2) 
ANS = 2 
>> V (. 4) 
error: V (. 4): OUT of bound. 3 
>> V (0) 
error: V (0): The subscripts MUST BE either integers. 1 to (2 ^ 63 is) - or logicals. 1 

% 2 column vector (separated by semicolons). 
>> V = [. 1; 2;. 3] 
V = 
   . 1 
   2 
   . 3 
  % to take the specified row column vector 
>> V (2) 
ANS = 2 
>> V (0) 
error: V (0): The subscripts MUST BE either integers. 1 to (2 ^ 63 is) or logicals -1 
>> V (. 4) 
error: V (. 4): OUT. 3 of bound

  

4. The matrix representation

  • Like with the vector, space or comma-separated columns, rows separated by semicolons
  • Colon: a: c indicates from a to c, step (interval) is 1; equivalent to a: >> A (2, 2)
  •          a: b: c indicates from a to c, step (interval) is b
>> A = [1,2;3,4]
A =
   1   2
   3   4
>> A = [1 3; 4 5]
A =
   1   3
   4   5

>> A = [1:2;4:6]   
error: vertical dimensions mismatch (1x2 vs 1x3)
>> A = [1:3;4:6]  %等价于  A = [1:1:3;4:1:6]
A =
   1   2   3
   4   5   6
>> A = [1:1:3;4:1:6]
A =
   1   2   3
   4   5   6
>> A = [1:3:7;5:2:9]
A =
   1   4   7
   5   7   9
>> A(2, 2)
years = 7
 >> A (1, :) 
ans = 

   1 4 7 

>> A (:, 1) 
ans = 

   1 
   5 

>> A (:, [1, 3]) 
ans = 

   1 7 
   May 9 

>> A ( [1,2], :) 
ans = 

   1 4 7 
   5 7 9 

>> A (:, :) 
ans = 

   1 4 7 
   5 7 9

  

The matrix calculation

% C = [AB], is applied to the B columns A, generating C (at this time the number of rows A and B must be the same) 
% C = [A; B], the B row is applied to the A, C generate (At this time, the number of columns a and B must be the same) 
>> a = [. 1] 
a =. 1 
>> a = [a, 2] 
a = 

   . 1 2 

>> a = [a;. 3] 
error: Vertical mismatch the Dimensions ( 1x1-VS 1x2) 
>> A = [A; [3,4-]] 
A = 

   . 1 2 
   . 3. 4 

>> A = [A, [. 5;. 6]] 
A = 

   . 1 2. 5 
   . 3. 4. 6 

>> B = [. 7 , 8, 9 are] 
B = 

   . 7. 8. 9 

>> [a; B] 
ANS = 

   . 1 2. 5 
   . 3. 4. 6 
   . 7. 8. 9 

into a vector of all the% data matrix 
>> a 
a = 

   . 1 2. 5 
   . 3. 4. 6 

>> A (:) 
Ans =

   . 1 
   . 3 
   2 
   . 4 
   . 5 
   . 6 

>> A (:) ' 
ANS = 

   . 1. 5. 4. 3 2. 6 

>> A 
A = 

   . 1 2. 5 
   . 3. 4. 6 


% assignment: 
>> A 
A = 

   . 1 2. 5 
   . 3. 4. 6 

>> A ( 1,2) =. 3 
A = 

   . 1. 3. 5 
   . 3. 4. 6 

>> A (. 1, :) = [0,0,0] 
A = 

   0 0 0 
   . 3. 4. 6 

>> A (2: 3,1: 2) = [1,1; 1,1]% a had no third row, the third row is added, to make up the remaining positions with 0 
a = 

   0 0 0 
   . 1. 1. 6 
   . 1. 1 0 

% of the matrix arithmetic method: 
> > A = [. 1. 1; 2,2 &] 
A = 

   . 1. 1 
   2 2

= B >> [3,3;. 4. 4] 
B = 

   . 3. 3 
   . 4. 4 

>> A + B 
ANS = 

   . 4. 4 
   . 6. 6 

>> A - B 
ANS = 

  -2 -2 
  -2 -2 

>> A - 2 
ANS = 

  -1 -1 
   0 0 

>> A * B 
ANS = 

    . 4. 6 
    . 8 12 is 

>> A * -A% corresponds -1 
ANS = 

  -1 -1 
  -2 -2 

>> A / 2 
ANS = 

   0.50000 0.50000 
   1.00000 1.00000 

>> B / a% Xa = b , X = b / a ( b represents a multiplied by the inverse) 
warning: Matrix Singular Precision Machine to 
ANS = 

   0.30000 0.60000 
   0.70000 1.40000

>> B \ A% aX = b , determined X. Then X = a \ b (a represents the inverse multiplied by B); 
ANS = 

   2.4672e-2.4672e-. 17. 17 
   5.0000e-01 5.0000e-01 

% point operations (* dot, point other ./.) 
  % Dimension same: multiplying corresponding elements of 
  the same row dimension%: corresponding to each row of elements is multiplied 
  % identical column dimension: multiplying the corresponding elements in each column 
  ..% B = A * A * B 
  % * multiplication; addition /; ^ square; et 
> > A 
A = 

   . 1. 1 
   2 2 

>> B 
B = 

   . 1 2 
   . 3. 4 

>> A. * B 
ANS = 

   . 1 2 
   . 6. 8 

>> A B ./ 
ANS = 

   1.00000 0.50000 
   .66667 0.50000


  

6. common special matrix

% Matrix 1. 
% Eye (N) to generate a unit matrix of N rows and N columns. 
% Eye (M, N) to generate a M rows and N columns "unit" matrix, the main diagonal element is 1, the remaining elements to zero. 
% ~ Eye (N) to generate a diagonal of N rows and N columns is 0, the remaining positions of the matrix are 1 
>> Eye (. 3) ANS = Diagonal the Matrix 1 0 0 0 1 0 0 0 1 >> Eye (2 ,. 3) ANS = Diagonal the matrix . 1 0 0 0 0. 1 >> Eye ~ (. 3) ANS = 0. 1. 1 . 1 0. 1 . 1. 1 0 % 2. transposed matrix >> A A = . 1. 4. 7 . 5. 7. 9 >> A ' ANS = . 1. 5 . 4. 7 . 7. 9 % 3. inverse matrix >> A A = . 1. 4. 7 . 7. 9. 5 % 7. the Gaussian distribution matrix: Pinv >> (A) ANS = -0.320513 0.230769 -0.051282 .076923 .217949 -0.076923 >> Pinv (A) * A ANS = 0.83333 -0.16667 0.33333 0.33333 0.33333 0.33333 -0.16667 0.33333 0.83333 % 4. Full Matrix 1: >> ones (. 3 , 2) ANS = . 1. 1 . 1. 1 . 1. 1 % 5. The all-zero matrix >> 0 * ones (2,3) ANS = 0 0 0 0 0 0 >> zeros (2,3) ANS = 0 0 0 0 0 0 % 6. random matrix a random number between 0 and 1 >> RAND (l, 3) ANS = 0.57432 0.81374 0.92313 >> randn (2,3) ANS = -0.057398 -0.526665 .918251 -0.218253 .984849 0.628937 >> randn (l, 3) ANS = -1.21260 -0.71408 1.06785 % 8. The cube matrix: % Magic is a function for generating cube matrix , each of its rows, columns and diagonals equal to the sum of the number. square matrix of n rows and n columns, and the value of 1 + 2 + 3 + ..... + n ^ and then divided by n, n must be an integer greater than or equal to 2 3. = ANS . 8. 6. 1 . 3. 5. 7 . 4. 9 2 >> Magic (. 4) ANS = 16 13 is 2. 3 . 5. 11. 8 10 . 9 12 is. 7. 6 . 4. 1 14 15

  

7. Control statements (to be continued)

 

Guess you like

Origin www.cnblogs.com/zoey12/p/11213578.html