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数据库完全在内存中,使用磁盘仅用 ...
随机推荐
- poj3268 Silver Cow Party(农场派对)
题目描述 原题来自:USACO 2007 Feb. Silver N(1≤N≤1000)N (1 \le N \le 1000)N(1≤N≤1000) 头牛要去参加一场在编号为 x(1≤x≤N)x(1 ...
- ASP.NET 构建高性能网站 架构设计
Web前端系统 为了达到不同应用的服务器共享.避免单点故障.集中管理.统一配置等目的,不以应用划分服 务器,而是将所有服务器做统一使用,每台服务器都可以对多个应用提供服务,当某些应用访问量升高时,通过 ...
- 【原】不定义Order属性,通过切面类的定义顺序来决定通知执行的先后顺序
[结论] 在多个切面类的“切入点相同”并且每个切面都“没有定义order属性”的情况下,则切面类(中的通知)的执行顺序与该切面类在<aop:config>元素中“声明的顺序”相关,即先声明 ...
- HAproxy和TIME WAIT的一次问题排查
近日平稳运行了将近4年的发号器突然出现问题,在元旦0分的时候出现短暂的性能下降,导致发号失败率飙高到一个不可接收的值,哎,意外总是发生在你想不到的地方. 这几天赶紧和小伙伴们赶紧追查原因,制定改造方案 ...
- 电感式DC/DC变换器工作原理
http://www.amobbs.com/thread-3293203-1-1.html 首先必须要了解电感的一些特性:电磁转换与磁储能.其它所有参数都是由这两个特性引出来的. 电感回路通电瞬间 断 ...
- .Net高级技术——垃圾收集器
垃圾收集器概述 大排档和学校食堂.一个是别人帮你收拾盘子,一个是自己收拾盘子. 垃圾收集GC(Garbage Collection).内存的分配.回收不需要程序员操心,程序员只要需要的时候new就可以 ...
- IOC详解和Unity基础使用介绍
说起IOC,可能很多初学者不知道是用来做什么的,今天正好有点时间,就来扫扫盲,顺便巩固下自己. IOC全称是Inversion Of Control,意为控制反转(这些自然百度也有),可什么是控制反转 ...
- Android中高亮变色显示文本中的关键字
应该是好久没有写有关技术类的文章了,前天还有人在群里问我,说群主很长时间没有分享干货了,今天分享一篇Android中TextView在大段的文字内容中如何让关键字高亮变色的文章 ,希望对大家有所帮助, ...
- 8个免费且实用的C++ GUI库(转载)
原文链接:http://zoomzum.com/8-free-and-useful-c-gui-libraries/ 作者的话:C++标准中并没有包含GUI,这也使得C++开发图形化界面需要依赖于 ...
- eclipse Reference 功能之——项目之间的引用
i'm sorry, i forgot this article where i found. that it is referenced. 以前也研究过Eclipse里Web Project引用Ja ...