吴恩达 ML 公开课笔记(4)-Octave abc
Contents
Too lazy to explain some of the commands…orz
Basic
PS1('sign')
Change prompt to ‘sign’- Matrix Assignment
v = [1 2; 3 4; 5 6]
v = [1.1 1.2 1.3] = [1:<0.1:>1.3]
(default step is 1)
- Matrix Generation commands
ones(2,3) = [1 1 1; 1 1 1]
(ones/zeros/rand/randn)eye(n)
: generate n by n identical matrixmagic(n)
: generate n by n magic matrix
size(M, n)
: return the size of the n=1:row n=2:coloumlength(M)
: max dimention of M
Moving data around
pwd
: show current pathload('xxx')
load <filename>
who
: display current scope. whos: detailsclear <variable>
: clear the space of the variableM(start:end)
: count from start to end coloum by coloumsave <filename> <variable> <encode>
: save variable to the file using enconde like ascii
Manipulate
M(x,y)
: indexingM(r1:r2,c1:c2)
sub-matrix block: coloumn1-2/row1-2.M(rn, :) = [a b c]
: assign [a b c] to row n of MM([r1 r2], :)
: copy row1 and row2M(M, [a b c])
: append [a b c] to MM(:)
: put all elements of M into a single vectorM = [X Y] = [X,Y]
: concatenating X & Y into M(horizontally)M = [X;Y]
: concatenating X & Y into M (vertically)
Computing on data
M'
: transposepinv(M)
: inverse of M- Element-wise calculation
A*b
,A .* B
: element-wise multiply, as well as.^
,./
,log(v)
,exp(v)
,abs(v)
and so onM < a
: element-wise comparison, which returns a boolean matrix
max()
[value, index] = max(M)
max(M,N)
: M and N are of the same size, do element-wise comparison, then generate a new matrixmax(M) = max(M,[],1)
: 1->coloum-wise; 2->per row-wise.
[r, c] = find(M < a)
: returns the index of the elementssum()
sum(M)
: summarize all the elementssum(M,1)
: summarize each coloumsum(M,2)
: summarize each row
prod(M) = prod(M,1)
: multiply each coloum. 1->coloum-wise; 2->per row-wise.floor(M)
: rounds downceil(M)
: rounds upflipud(M)
: flip upside-down
Plotting data
hist(v, n)
: plot v with n buckets histogramplot(x, y, <color>)
plot(x,y1) hold; plot(x,y2)
xlabel('X') ylabel('Y')
legend('y1','y2')
title('title')
print -dpng 'filename.png'
close: close figure window
figure <number>; plot(x,y1);figure <number>; plot(x,y2);
…subplot(x,y,index)
: access the indexaxis([xmin xmax ymin ymax])
clf
: clear all figureimagesc(M), colorbar, colormap gray;
Control statements
for i=1:10, v(i) = 2^i; end;
break
continue
while i<=5, v(i)=100; i=i+1; end;
if i==6, <stat>; elseif i==7, <stat2>; else <stat3>; end;
Functions
<functionname.m>
: format of the function file- Example:
|
|
- addpath(‘path’)