CheeseZH: Octave basic commands

  1 1.Basic Operations
  2 5+6
  3 3-2
  4 5*8
  5 1/2
  6 2^6
  7 1 == 2 %false  ans = 0
  8 1 ~= 2 %true ans = 1
  9 1 && 0 %AND ans = 0
 10 1 || 0 %OR ans = 1
 11 xor(1,0) %ans = 1
 12 PS1(‘>> ‘); %change the command prompt info
 13 a = 3 %show a = 3 in screen
 14 a = 3 %not show a = 3 in screen
 15 b = ‘hi‘
 16 c = (3>=1)
 17 a = pi; %a = 3.1416
 18 disp(sprintf(‘2 decimals: %0.2f‘,a)) %2 decimals: 3.14
 19 disp(sprintf(‘6 decimals: %0.6f‘,a)) %2 decimals: 3.141593
 20 format long  %a = 3.14159265358979
 21 format short %a = 3.1416
 22
 23 2. Matrices and Vectors
 24 A = [1 2; 3 4; 5 6]
 25 v = [ 1 2 3]
 26 v = [1; 2; 3]
 27 v = 1:0.1:2 %1.0 1.1 1.2 ... 2.0
 28 v = 1:6
 29 v = ones(2,3)
 30 c = 2*ones(2,3)
 31 w = ones(1,3)
 32 w = zeros(1,3)
 33 w = rand(1,3)
 34 w = rand(3,3)
 35 w = randn(1,3) %Gossian distribution
 36 w = -6 + sqrt(10)*(randn(1,10000))
 37 hist(w) %draw a histogram
 38 hist(w,50)
 39 I = eye(4)
 40 help eye
 41 help rand
 42 help help
 43
 44 3.Moving data around
 45 A = [1 2; 3 4; 5 6]
 46 size(A) %ans = 3 2
 47 size(A,1) %ans = 3
 48 size(A,2) %ans = 2
 49 A(3,2) %ans = 6
 50 A(2,:) %the second row, ‘:‘ means every elements along that row/column
 51 A(:,2) %the second column
 52 A([1 3],:) %the first and the third row
 53 A(:,2) = [10; 11; 12]
 54 A = [A, [100; 101; 102]] %append another column vector to right
 55 A(:) %put all elements of A into a single vector
 56 A = [1 2; 3 4; 5 6]
 57 B = [11 12; 13 14; 15 16]
 58 C = [A B] %3 * 4
 59 C = [A; B] %6 * 2
 60
 61
 62 v = [1 2 3 4]
 63 length(v) %ans = 4
 64 length(A) %ans = 3
 65 length([1;2;3;4;5]) %ans = 5
 66
 67 pwd %current path
 68 cd ‘path...‘
 69 ls
 70
 71 load featuresX.dat
 72 load priceY.dat
 73 load(‘featuresX.dat‘)
 74 load(‘priceY.dat‘)
 75
 76 who %show the variables in current scope
 77 whos %show the variables detail in current scope
 78 size(featuresX) %ans = 47 2
 79
 80 v = priceY(1:10)
 81
 82 save hello.mat v; %save v into a file named "hello.mat"(BINARY)
 83 save hello.txt v; %txt format can be understood by human(ASCII)
 84 clear featuresX %delete featuresX from current scope
 85 clear %delete all variables in current scope
 86
 87 4.Computing on data
 88 A = [1 2; 3 4; 5 6]
 89 B = [11 12; 13 14; 15 16]
 90 C = [1 1; 2 2]
 91 A*C
 92 A.*B %‘.‘ element operation
 93 A.^2
 94 v = [1; 2; 3]
 95 1./v
 96 log(v)
 97 exp(v)
 98 abs(v)
 99 -v %-1*v
100 v + ones(length(v),1) % v + 1, v + ones(3,1)
101 A‘ %transpose
102
103 a = [1 15 2 0.5]
104 val = max(a) %val = 15
105 [val, ind] = max(a) %val=15 ind(ex)=2
106 max(A) %ans = 5 6
107 a < 3 %element wise comparison: ans = 1 0 1 1
108 find(a<3) %ans = 1 3 4
109 A = magic(3)
110 [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
111 help find
112 sum(a)
113 prod(a)
114 floor(a)
115 ceil(a)
116 max(rand(3),rand(3))
117 max(A,[],1) %the maximum element in each column
118 max(A,[],2) %the maximum element in each row
119 max(A) %ans = 8 9 7
120 max(max(A)) %ans = 9
121 max(A(:)) %ans = 9
122 A = magic(9)
123 sum(A,1) %sum up each column
124 sum(A,2) %sum up each row
125 sum(sum(A.*eye(9))) %sum up the main diagonal
126 sum(sum(A.*flipud(eye(9)))) %sum up the other diagonal
127 pinv(A) %pseudo inverse
128
129
130 5. Plotting Data
131 t = [0:0.01:0.98]
132 y1 = sin(2*pi*4*t)
133 plot(t,y1);%sin function
134 y2 = cos(2*pi*4*t)
135 hold on; %put figures in one window
136 plot(t,y2,‘r‘) %change the color of cos to red
137 xlabel(‘time‘)
138 ylabel(‘value‘)
139 legend(‘sin‘,‘cos‘)
140 title(‘my plot‘)
141 cd ‘/home/zhanghe‘;
142 print -dpng ‘myPlot.png‘
143 close
144 figure(1);plot(t,y1);
145 figure(2);plot(t,y2);
146 subplot(1,2,1) %%divides plot into 1*2 grid access first element
147 plot(t,y1);
148 subplot(1,2,2) %%divides plot into 1*2 grid access second element
149 plot(t,y2);
150 axis([0.5 1 -1 1])
151 clf;
152 A = magic(5)
153 imagesc(A)
154 imagesc(A),colorbar,colormap
155
156
157 6. Control statements
158 v = zeros(10,1)
159 for i=1:10,
160   v(i) = 2^i;
161 end;
162 indices = 1:10;
163 for i=indices,
164   disp(i);
165 end;
166 i = 1;
167 while i<=5,
168   v(i) = 100;
169   i = i+1;
170 end;
171 while true,
172   v(i) = 999;
173   i = i+1;
174   if i==6,
175     break;
176   end;
177 end;
178 v(1) = 2;
179 if v(1) == 1,
180   disp(‘One‘);
181 elseif v(1) == 2,
182   disp(‘Two‘);
183 else
184   disp(‘Else‘);
185 end;
186
187 %Octave search path (advanced/optional)
188 addpath(‘/home/zhanghe/ml/ex1‘)
189
190 %Example of CostFunction
191 predictions = X*theta;
192 sqrErrors = (predictions-y).^2;
193 J = 1/(2*m) * sum(sqrErrors);
194
195 7.Vectorization
196 % Hypothesis Function
197 % Unvectorized implementation
198 prediction = 0.0;
199 for j = 1:n+1,
200   prediction = prediction + theta(j) * x(j)
201 end;
202 % Vectorized implementation
203 prediction = theta‘ * x
204
205 % Gradient descent(Simultaneous updates)
206 % Vectorized implementation
207 delta = 1/m*((hypothesis-y)*x)
208 theta = theta - alpha*delta
时间: 2024-10-15 10:08:41

CheeseZH: Octave basic commands的相关文章

Network Basic Commands Summary

Network Basic Commands Summary set or modify hostname a)     temporary ways hostname NEW_HOSTNAME, but if you reboot your system, it will disabled. b)    permanent ways: edit "/etc/sysconfig/network" HOSTNAME, then restart system, it will effect

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 Linux? Linux is a Unix-like open source operating system. At the core of the operating system is the Linux kernel. It acts as the intermediary between t

unix basic commands

1. man - an interface to the on-line reference manuals $man man 2. apt - advanced package tool SEE ALSO: apt-cache, apt-get, apt.conf, sources.list $apt-cache search mysql 3.sudo - execute a command as another user (sudo allows a permitted user to ex

quagga源码分析--通用库command

quagga是一个完整又成熟的系统,作为一个路由器软件,自然要提供人机接口. quagga提供snmp管理接口,而且,自然就会有对应的命令行管理格式,当然一般路由软件不会提供界面形式的,也许有webui,然而quagga并没有. 我们要看的就是这个命令行处理的代码 command. 接触过类似命令行的朋友肯定有一点点好奇吧,那么数量庞大的命令和参数输入,还可以提供提示和自动补齐,这肯定不是一件很简单的事情. 下面是一个配置示例: 1 ! 2 interface bge0 3 ip ospf au

Squid Proxy Server 3.1

Improve the performance of your network using the caching and access control capabilitiess of squid. A beginner level knowledge of Linux/Unix operating system familiarity with basic commands is all what you need.Squid runs almost on all Linux/Unix op

突击Mercurial SCM(HG)

这个叫水银的源码管理工具虽然默默无闻,但还是得到了很多团队的使用.为了迎合某些团队的需要,我们也要用它来管理我们的代码. 今天的任务是先突击学习,磨刀不误砍柴工.对工具的掌握越快,工作的效率就会越高. 1.安装 首先从官网下载最新的版本,我这次做个实验,下载了3.2-rc. 解压到你指定的目录下: [[email protected] mercurial]$ ls mercurial-3.2-rc.tar.gz [[email protected] mercurial]$ tar xzvf me

Mac环境 go语言之入门HelloWorld

1. 安装mercurial Mercurial 是一种轻量级分布式版本控制系统,采用 Python 语言实现 可以输入hg命令查询系统是否安装mercurial,可以如下两种命令安装 $sudo pip install mercurial 安装成功之后 $sudo easy-install mercurial 安装之后,输入hg命令,如下内容显示,表示成功安装 mercuria $ hg Mercurial Distributed SCM basic commands: add        

Redis 哈希(Hash)

Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿). 实例 redis 127.0.0.1:6379> HMSET w3ckey name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000 OK redi

Debug with jdb

原文地址: http://www.javaworld.com/article/2077445/testing-debugging/debug-with-jdb.html Q: How do you use jdb (included in the JDK 1.2 package) effectively to debug Java programs? I've tried many times, but I am successful only in loading a class file t