Machine Learning-Overview of common matlab programming commands
-- Summary from ng-ml-class octave/MATLAB tutorial Coursera
A. basic operations and moving data around1 in command line mode, you can use Shift + press enter to append the next line to output 2 length command to apply to the matrix, and return a higher one-dimensional dimension3 help + command is the display command. mat File Save hello. mat B uses binary compression to save the data. Save hello.txt V-ASCII is saved as a readable file. That is, the text format is 6: means every elements in this col7 A ([1 3], obtain data of all columns in rows 1st and 3. 8 A = [A, [100; 101; 102] Add a column Col vector [100,101,102] 9 size (a) After matrix) returns a matrix of 1 rows and 2 columns, indicating that the size of 1st and 2nd dimenxes is 10 C = [a B] equivalent to C = [a, B, concatenate the two matrices [] into the Concat Connection Matrix or the string 11 c = [A; B]; number indicates to add to the following row, so the corresponding number of rows is increased, the number of columns remains unchanged. B. computing on data12. * B is matrix/vector point multiplication a * B is matrix multiplication 13 log (V) and exp (v) Evaluate the logarithm and exponent 14 ABS () Based on E () returns the value and index of the maximum element in the matrix. [Val, IND] = max () 17 A <3 determines whether each of the values of a is less than 3. If the value is less than 3, true is returned for the corresponding position. Otherwise, false18 find (A <3) is returned for the position) returns the index 19 magic square with all values less than 3 in the matrix.
Magic (n) is an N-by-n matrix constructed from the Integers
1 through N ^ 2 with equal row, column, and diagonal sums.
Produces valid Magic Squares for all N> 0 records t n = 2.20 [R, C] = find (A> = 7) returns the row of an element greater than or equal to 7 and the index of Col 21. Prod (a) calculates the product of all elements in matrix A. Floor () round-down integer Ceil (a) For elements in matrix A round-up integer rand (3) to generate a random matrix max (A, [], 1) of 3x3) evaluate the maximum value of each column of matrix A (1 indicates Dimension 1) max (A, [], 2) for each row of matrix A sum (, 1) sum the first dimension (that is, each column) of matrix A (note that the first dimension in MATLAB is a column by default, followed by a row, and so on ...) Sum (A, 2) Sums sum (. * eye (9) is used to calculate the sum of diagonal elements of matrix A. 22. flipud flip matrix in up/down direction. flip the matrix up and down, similar to fliplr, rot90, flipdim.
Flipud (x) returns X with columns preserved and rows flipped
In the up/down direction. For example,
X = 1 4 becomes 3 6
2 5 2 5
3 6 1 423 pinv (A) and inv (a) Calculate the inverse matrices C and plo1_data24 t of matrix A = [0.1: 0.01: 0.98] Y = sin (t) plot (T, Y) Draw a sine curve 25 hold on; retain the current curve, draw a curve 26 x label calibration X axis description 27 legend ('sin', 'cos ') add legend 28 title ('My plot') Add picture title 29 print-dpng 'myplot.png 'Save picture 30 line color annotation control B blue. point-Solid
G green o circle: dotted
R red X-mark-. dashdot
C cyan + plus -- dashed
M magenta * Star (none) No line
Y yellow S Square
K black D Diamond
W white V triangle (down)
^ Triangle (up)
<Triangle (left)
> Triangle (right)
P pentagram
H hexagram31 subplot to draw a subgraph. Multiple graphs are merged into one graph> subplot (, 1) % divides plot a 1X2 grid, access the 1st Element
> Plot (T, Y1)
> Subplot (1, 2)
> Plot (T, Y2) 32 grid plus grid 33 figure (1)
> Plot (T, Y1)
> Figure (2)
> Plot (T, Y2) draws the curves of Y1 and Y2 to 34 axis ([0.5 1-1]) in the two files named Figure1 and figure2. set the X axis to 0.5 ~ 1. the Y axis ranges from-1 ~ 135 CLF clear current figure.36 imagesc (a) imagesc Scale Data and display as image. draws matrix A into a small color square 37 imagesc (A), colorbar, colormap gray; colorbar displays the color gradient bar, color Description colormap sets colormap properties, that is, RGB three-color a color map matrix may have any number of rows, but it must have
Exactly 3 columns. Each row is interpreted as a color, with
First element specifying the intensity of red light, the second
Green, and the third blue. Color intensity can be specified on
Interval 0.0 to 1.0.38 A = 1; B = 2; C = 3; the values a = 1, B = 2, and a B C are not displayed, C = 3 will output the values D, control statements: For, while, if statements39 for loop for I =,
V (I) = 2 ^ I;
End;
> V
V =
2
4
8
16
32
64
128
256
512
1024> indices =
For I = indices,
Disp (I)
End; 40 while loop> while I <= 5,
V (I) = 100;
I = I + 1;
End;
> V
V =
100
100
100
100
100
64
128
256
512
1024
> I = 1;
> While true,
V (I) = 999;
I = I + 1;
If I = 6,
Break;
End;
End;
> Exit
V =
999
999
999
999
999
64
128
256
512
102441 if elseif judge branch statement V (1) = 2
V =
2
999
999
999
999
64
128
256
512
1024
> If V (1) = 1,
Disp ('the value is one ');
Elseif V (1) = 2,
Disp ('the value is two ');
Else
Disp ('the value is not one or two .');
End;
The value is the definition of the two42 function and the use of the definition function squarethisnumber. M file Function Y = squarethisnumber (x) y = x ^ 2; call squarethisnumber (5)
Ans =
25
43 addpath adds the path of the MATLAB search function 44 the function defined in MATLAB can return more than one value, which is different from C ++ and other programming languages, the function in C \ c ++ has a unique return value that allows multiple return values to facilitate programming. function [a, B] = squareandcubethisnumber (x) A = x ^ 2; B = x ^ 3; call> [x1, x2] = squareandcubethisnumber (5)
X1 =
25
X2 =
125
45 Cost Function J function example Function J = costfunctionj (X, Y, theta) % x is the "design matrix" containing our training examples. % Y is the class labels M = size (x, 1); % Number of training examplespredictions = x * Theta; % predictions of hypothesis on all M examplessqrerrors = (predictions-Y ). ^ 2; % squared errorsj = 1/(2 * m) * sum (sqrerrors );
Call X = [1 1; 1 2; 1 3]; y = [1; 2; 3];
Theta = [0; 1];
> J = costfunctionj (X, Y, theta)
J =
0
% Squared errors is 0 in this example> Theta = [0; 0]
Theta =
0
0
> J = costfunctionj (X, Y, theta)
J =
2.3333
% Squared errors is 2.3333 in this example % which is (1 ^ 2 + 2 ^ 2 + 3 ^ 2)/(2*3) = 2.3333
E. vectorizationthe implementation algorithm of "vectorizationmatlab" can reduce unnecessary loops and calculate the values of all vectors in the same way. This is a faster and more efficient algorithm implementation idea, be aware of the differences between the algorithm and other programming languages Java, C, and C ++ in the "vectorized" and "matrix" operation variables in MATLAB.