Octave Tutorial
Octave 语法几乎和Matelab一样
- 一般的数学计算(elementary math operations)与python几乎一样:+、-、*、\、^
- 逻辑运算(logical operations):
==~=-
\&\&:AND -
||:OR;xor(0,1)等价于 0||1 - $$
-
:comment
-
PS1('>> '):可以简化命令行成'>> ' -
disp():打印字符串或者对象等 - 向量、矩阵和数组:
-
v=[1 2;3 4;5 6;]:向量 -
v=1:0.1:2:从1到2步长位0.1的数组 -
v=1:6:默认步长为1 -
2*ones(2,3):生成2*3的矩阵,都是2,默认ones(2,3)都是1; -
zeros(1,3)默认都是0 -
rand(1,3)随机在0-1之间的数值 -
randn(3,3)高斯分布的随机值,均值为0,标准差为1 -
hist(w)打印直方图,w为一个向量,默认为5条,hist(w,n)可以打印出n条来 -
eye(n)生成一个n维的单位矩阵
-
-
help eye生成帮助信息
load data
-
size(A):returns the size of the matrix A(returns a matrix).size(A,1):the size of the first dimension of A;size(A,2)the number of columms in the matrix A. -
length(v):the longer dimension of v. -
pwd:show the current path.cd:change a directory.ls:list all the directory of this path. -
load featuresX.datorload('featuresX.dat') -
whovariables in current scope.whosdetailed view. -
clear Xget rid of a variable X.clearclear all the variables. -
save hello.mat vsave the variable v to hello.mat.This commend will save data in a binary format.save hello.txt v -asciisave data in a text format. -
A(:,2)the colon (:) means every elemet along the second column.A([1 3],:)get everything from the first row and the third row.A(:,2) = [10;11;12]assigment the sencond column of the matrix.A = [A,[10,11,12]]append a column in the right. -
A(:)put all elements of A into a single vector.
Computing on data
-
A*C: matrix mulitipation. -
A.*B: elements wise operations -
A./B: -
1./B: the dot means emement wise operations in octave. -
A.^2: the square of each elemnets. -
log(v): -
abs(v): the absolute value of each element in v. -
exp(v): -
-v=-1*v: -
v+ones(length(v),1)=v+1: -
A': a transpose of A. -
val=max(A): the column wise maximum. -
[val,ind]=max(A): the maximum and the index. -
A < 2: elements wise compariation(0 or 1).false or true. -
find(A<2): return the index of which is true less then 2. -
magic(3): create an N-by-N magic square.All the rows columns and diagnoals sum up to the same thing. -
(r,c) = find(A>1): returnes the rows and columns -
sum(A): return the sum of A.prod(a): returns the product of A.floor(A): rounds down all the elemments of A.ceil(A): rounds up . -
max(rand(3),rand(3)): returns a ramdomly matrix ,each element is the maxium of two radomly generated matrices. -
max(A,[],1): the max of each column. This 1 means take the max along the first dimension of A. -
flipup(A): flip up the matrix A.
Plotting data
When using ';', there will be no data show in the cmd.
example:
t=[0;0.01;0.98];
y1=sin(2*pi*4*t);
plot(t,y1);
y2=cos(2*pi*4*t);
plot(t,y2);
plot(t,y1);
hold on;
plot(t,y2,'r');
xlabel('time');
ylabel('value);
legend('sin','cos')
title('my plot')
print -dpng 'my plot.png'
figure(1);plot(t,y1);
figure(2);plot(t,y2);% 可以出现两张图
subplot(1,2,1);% Divides plot a 1x2 grid, access first element.
axis([0.5 1 -1 1]) % set the x range and y range for the figure
clf
A = magic(5)
imagesc(A)
imagesc(A),colorbar,colormap gray;
clf: clear a fiugere.
close all: close all fiugres.
a=1,b=2,c=3;% comman chaining of function calls.
For, while, if statements, and functions
v=zeros(10,1)
for i-1:10,
v(i)=^i;
end;
indices=1:10;
for i=indices,
disp(i);
end;
i=1;
while 9<=5,
v(i)=100;
i=i+1;
end;
i=1;
while true,
v(i)= 999;
i=i+1;
if i == 6,
break;
end;
end;
v(1)=2;
if v(i)===1,
disp('The value is 1');
elseif v(i)==2,
disp('The value is 2');
else
disp('The value is not one or two.');
end;
定义函数:
- 文件名要与函数名一致,后缀为
.m。 - 第一行
function y = functionName(x) - 第三行函数体
y = x^2;,这是一个平方函数。 - 使用的时候,需要用
cd打开目录,或者用addpath('path')把路径加入进去。 - 可以定义返回多个变量的函数
function [y1,y2] = functionName(x)
一个简单的代价函数计算函数:
function J = costFunctionJ(x,y,theta)
% x is the "design matrix" containing our training examples.
% y is the class labels.
m = size(x,1); % nmuber of training examples
predictions = x*theta; % predictions of hypothesis on all m examples
sqrErrors = (predictions-y).^2; % squared errors
J = 1/(2*m) * sum(sqrErrors);
Vectorization
- example:
- example2:两个变量的预测







网友评论