您的位置:首页 > 其它

CheeseZH: Octave basic commands

2015-06-24 09:07 375 查看
1.Basic Operations
5+6
3-2
5*8
1/2
2^6
1 == 2 %false  ans = 0
1 ~= 2 %true ans = 1
1 && 0 %AND ans = 0
1 || 0 %OR ans = 1
xor(1,0) %ans = 1
PS1('>> '); %change the command prompt info
a = 3 %show a = 3 in screen
a = 3 %not show a = 3 in screen
b = 'hi'
c = (3>=1)
a = pi; %a = 3.1416
disp(sprintf('2 decimals: %0.2f',a)) %2 decimals: 3.14
disp(sprintf('6 decimals: %0.6f',a)) %2 decimals: 3.141593
format long  %a = 3.14159265358979
format short %a = 3.1416

2. Matrices and Vectors
A = [1 2; 3 4; 5 6]
v = [ 1 2 3]
v = [1; 2; 3]
v = 1:0.1:2 %1.0 1.1 1.2 ... 2.0
v = 1:6
v = ones(2,3)
c = 2*ones(2,3)
w = ones(1,3)
w = zeros(1,3)
w = rand(1,3)
w = rand(3,3)
w = randn(1,3) %Gossian distribution
w = -6 + sqrt(10)*(randn(1,10000))
hist(w) %draw a histogram
hist(w,50)
I = eye(4)
help eye
help rand
help help

3.Moving data around
A = [1 2; 3 4; 5 6]
size(A) %ans = 3 2
size(A,1) %ans = 3
size(A,2) %ans = 2
A(3,2) %ans = 6
A(2,:) %the second row, ':' means every elements along that row/column
A(:,2) %the second column
A([1 3],:) %the first and the third row
A(:,2) = [10; 11; 12]
A = [A, [100; 101; 102]] %append another column vector to right
A(:) %put all elements of A into a single vector
A = [1 2; 3 4; 5 6]
B = [11 12; 13 14; 15 16]
C = [A B] %3 * 4
C = [A; B] %6 * 2

v = [1 2 3 4]
length(v) %ans = 4
length(A) %ans = 3
length([1;2;3;4;5]) %ans = 5

pwd %current path
cd 'path...'
ls

load featuresX.dat
load priceY.dat
load('featuresX.dat')
load('priceY.dat')

who %show the variables in current scope
whos %show the variables detail in current scope
size(featuresX) %ans = 47 2

v = priceY(1:10)

save hello.mat v; %save v into a file named "hello.mat"(BINARY)
save hello.txt v; %txt format can be understood by human(ASCII)
clear featuresX %delete featuresX from current scope
clear %delete all variables in current scope

4.Computing on data
A = [1 2; 3 4; 5 6]
B = [11 12; 13 14; 15 16]
C = [1 1; 2 2]
A*C
A.*B %'.' element operation
A.^2
v = [1; 2; 3]
1./v
log(v)
exp(v)
abs(v)
-v %-1*v
v + ones(length(v),1) % v + 1, v + ones(3,1)
A' %transpose

a = [1 15 2 0.5]
val = max(a) %val = 15
[val, ind] = max(a) %val=15 ind(ex)=2
max(A) %ans = 5 6
a < 3 %element wise comparison: ans = 1 0 1 1
find(a<3) %ans = 1 3 4
A = magic(3)
[r, c] = find(A>=7) % r = 1; 3; 2   c = 1; 2; 3 ==>A(1,1) A(3,2) A(2,3) are greater than 7
help find
sum(a)
prod(a)
floor(a)
ceil(a)
max(rand(3),rand(3))
max(A,[],1) %the maximum element in each column
max(A,[],2) %the maximum element in each row
max(A) %ans = 8 9 7
max(max(A)) %ans = 9
max(A(:)) %ans = 9
A = magic(9)
sum(A,1) %sum up each column
sum(A,2) %sum up each row
sum(sum(A.*eye(9))) %sum up the main diagonal
sum(sum(A.*flipud(eye(9)))) %sum up the other diagonal
pinv(A) %pseudo inverse

5. Plotting Data
t = [0:0.01:0.98]
y1 = sin(2*pi*4*t)
plot(t,y1);%sin function
y2 = cos(2*pi*4*t)
hold on; %put figures in one window
plot(t,y2,'r') %change the color of cos to red
xlabel('time')
ylabel('value')
legend('sin','cos')
title('my plot')
cd '/home/zhanghe';
print -dpng 'myPlot.png'
close
figure(1);plot(t,y1);
figure(2);plot(t,y2);
subplot(1,2,1) %%divides plot into 1*2 grid access first element
plot(t,y1);
subplot(1,2,2) %%divides plot into 1*2 grid access second element
plot(t,y2);
axis([0.5 1 -1 1])
clf;
A = magic(5)
imagesc(A)
imagesc(A),colorbar,colormap

6. Control statements
v = zeros(10,1)
for i=1:10,
v(i) = 2^i;
end;
indices = 1:10;
for i=indices,
disp(i);
end;
i = 1;
while i<=5,
v(i) = 100;
i = i+1;
end;
while true,
v(i) = 999;
i = i+1;
if i==6,
break;
end;
end;
v(1) = 2;
if v(1) == 1,
disp('One');
elseif v(1) == 2,
disp('Two');
else
disp('Else');
end;

%Octave search path (advanced/optional)
addpath('/home/zhanghe/ml/ex1')

%Example of CostFunction
predictions = X*theta;
sqrErrors = (predictions-y).^2;
J = 1/(2*m) * sum(sqrErrors);

7.Vectorization
% Hypothesis Function
% Unvectorized implementation
prediction = 0.0;
for j = 1:n+1,
prediction = prediction + theta(j) * x(j)
end;
% Vectorized implementation
prediction = theta' * x

% Gradient descent(Simultaneous updates)
% Vectorized implementation
delta = 1/m*((hypothesis-y)*x)
theta = theta - alpha*delta
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: