CheeseZH: Octave basic commands
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
CheeseZH: Octave basic commands的更多相关文章
- Network Basic Commands Summary
Network Basic Commands Summary set or modify hostname a) temporary ways hostname NEW_HOSTNAME, b ...
- Postgres Basic Commands for Beginners
Just sharing what I have learned about postgres recently. Here is a part of basic commands you may n ...
- Linux--Introduction and Basic commands(Part one)
Welcome to Linux world! Introduction and Basic commands--Part one J.C 2018.3.11 Chapter 1 What Is Li ...
- linux basic commands
1. man - an interface to the on-line reference manuals $man man 2. apt - advanced package tool SEE A ...
- kafka basic commands
kafka-server-start.sh config/server.properties & kafka-server-stop.sh kafka-topics.sh --creat ...
- Kali Basic Configuration
1:Kali Version root@kali-node01:~# cat /etc/os-release PRETTY_NAME="Kali GNU/Linux Rolling" ...
- 13 Basic Cat Command Examples in Linux
FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...
- [C2P1] Andrew Ng - Machine Learning
About this Course Machine learning is the science of getting computers to act without being explicit ...
- 【转】Redis入门
Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...
随机推荐
- Jenkins 使用 maven 出现C:\Windows\system32\config\systemprofile的解决
jenkins 使用 maven 出现 C:\Windows\system32\config\systemprofile 的原因是 Jenkins 服务启动的账号使用了系统的账号,在服务里改成具体的桌 ...
- the elements of computing systems 的读书笔记2
懒癌发作,本来计划是两到三天就一个unit的,没想到一直拖到今天才完成第二部分(6-8章). 第6章,介绍了hack汇编到二进制,也就是用翻译到01来表示.从课后习题来看,这一章目的就是设计一个程序( ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B. Problems for Round 水题
B. Problems for Round 题目连接: http://www.codeforces.com/contest/673/problem/B Description There are n ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- opencv第二课 进行缩放图片~
#include<stdio.h> #include<iostream> #include<opencv2\opencv.hpp> using namespace ...
- 手把手教你搭建Docker私有仓库
章节一:centos7 docker安装和使用_入门教程 章节二:使用docker部署Asp.net core web应用程序 有了前面的基础,接下来的操作就比较简单了.先准备两台虚拟机,两台机器上都 ...
- MySQL的转义符 ` 作用
` 是 MySQL 的转义符,避免和 mysql 的本身的关键字冲突,只要你不在列名.表名中使用 mysql 的保留字或中文,就不需要转义. 所有的数据库都有类似的设置,不过mysql用的是`而已.通 ...
- configure: error: lzo enabled but missing
注意:yum安装的无效,需要手动下载源码安装. wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz tar zx ...
- 强烈推荐:Android史上最强大的自定义任务软件Tasker
强烈推荐:Android史上最强大的自定义任务软件Taskerhttp://bbs.mumayi.com/thread-28387-1-1.html(出处: 木蚂蚁手机乐园) Android上的Tas ...
- 字符串变量作mysql查询条件
原文:http://blog.csdn.net/qing_gee/article/details/41646503 当你的查询条件是一个字符串变量时,你该怎么办,比如字符串可能是“0001ME,000 ...