Installing Octave on Windows
Use this link to install Octave for windows: http://wiki.octave.org/Octave_for_Microsoft_Windows
Octave on Windows can be used to submit programming assignments in this course but will likely need a patch provided in the discussion forum. Refer to https://www.coursera.org/learn/machine-learning/discussions/vgCyrQoMEeWv5yIAC00Eog? for more information about the patch for your version.
"Warning: Do not install Octave 4.0.0"; checkout the "Resources" menu's section of "Installation Issues".
4.0.0版本有重大bug,但在4.0.1上修复了。
在命令行输入 octave 进入 octave-gui
help [function name] 查看函数说明
cd xxx 进入工作目录
pwd 显示当前目录
PS1('>> '); 开头只显示>>符号
clc 清屏
clear 清除所有变量
clear a 清除 a 变量
who 显示当前工作区所有已经定义的变量名
whos 显示当前工作区所有已经定义的变量详细信息
% 表示注释
最后输入分号; 表示抑制输出
运算符:
+、-、*、/、^ (x^n 代表 x 的 n 次方)
逻辑运算符
==、~=(不等于)、&&、||、xor(a, b)
pi 表示
disp(a) 输出 a
sprintf('%.2f') 格式化字符串
format long 输出小数多位
format short 输出小数 4 位
定义矩阵:
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> A = [1 2;
> 3 4;
> 5 6;
> ]
A =
1 2
3 4
5 6
利用循环机制定义向量:
>> v = 1: 0.1: 1.5
v =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000
>> v = 1 : 6
v =
1 2 3 4 5 6
ones(m, n) 定义一个全 1 的mXn矩阵
zeros(m, n) 定义一个全 0 的mXn矩阵
rand(m, n) 定义一个随机值[0, 1)的 mXn 矩阵
randn(m, n) 定义一个高斯分布的随机值得 mXn 矩阵
sqrt(x) 根号x
hist(v[, 50]) 显示向量 v 的高斯分布直方图
eye(m) 定义一个 mXm 的单位矩阵
size(A) 返回向量[m, n]
size(A, 1) 返回 m
size(A, 2) 返回 n
length(v) 返回最长的维度
numel(A) 返回A的元素总个数
load featureX.dat 加载文件为变量
load('featureX.dat') 加载文件为变量
save hello.mat v; 将 v 向量保存到 hello.mat文件
save hello.txt v -ascii; 将 v 向量保存到 hello.txt文件以 ASCII 编码格式
A(i1 : j1, i2 : j2) 选取[i1, j1]行[i2, j2]列的元素
A([1 3], :) 选取第 1、3行所有元素
A = [A, [x1; x2; ...xm]] 在 A 右边加上一列
A = [A; [x1, x2, ...xn]] 在 A 下边加上一行
A(:) 表示 A 的所有元素在一个 m*n 行 1 列的向量里
矩阵运算:
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> B = [11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
>> A * B
error: operator *: nonconformant arguments (op1 is 3x2, op2 is 3x2)
>> A' * B
ans =
125 134
164 176
>> A .* B
ans =
11 24
39 56
75 96
>> A .^ 2
ans =
1 4
9 16
25 36
>> A ^ 2
error: for x^y, only square matrix arguments are permitted and one argument must be scalar. Use .^ for elementwise power.
>> 1 / A
error: operator /: nonconformant arguments (op1 is 1x1, op2 is 3x2)
>> 1 ./ A
ans =
1.00000 0.50000
0.33333 0.25000
0.20000 0.16667
log(v) 对 v 向量每个值求log(x)
exp(v) 对 v 向量每个值求
abs(v) 对 v 向量每个值求绝对值
max(v) 求最大值
sum(v) 累和
mean(v/A, 1/2) 求平均值
prod(v) 累积
floor(v) 舍位
ceil(v) 进位
[val, index] = max(v) 求 v 向量最大值和最大值下标
v < scala 小于的就是 1 否则是 0
find(v < scala) 返回小于的下标
magic(3) 返回 3 行 3 列的魔法矩阵,即每行每列对角线的和相等
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> [row, col] = find(A >= 7)
row =
1
3
2
col =
1
2
3
max(A, B) 分别比较 A、B 各个元素大小
>> A= magic(3)
A =
8 1 6
3 5 7
4 9 2
>> max(A)
ans =
8 9 7
>> max(A, [], 1)
ans =
8 9 7
>> max(A, [], 2)
ans =
8
7
9
>> max(A(:))
ans =
9
>> sum(A, 1)
ans =
15 15 15
>> sum(A, 2)
ans =
15
15
15
flipud(A) 上下反转
pinv(A) A的逆矩阵
绘图:
>> t = [0 : 0.01: 0.98];
>> y1 = sin(2*pi*4*t);
>> plot(t, y1);
>> y2 = cos(2*pi*4*t);
>> plot(t, y2);
>> t = [0 : 0.01: 0.98];
>> y1 = sin(2*pi*4*t);
>> plot(t, y1);
>> y2 = cos(2*pi*4*t);
>> plot(t, y2);
>> % hold on 对应 hold off
>> plot(t, y1);
>> hold on;
>> plot(t, y2, 'r'); % red color
>> % plot(t, y2, 'rx', 'MarkerSize', 10); % 绘制离散点
>> xlabel('time'); % x 轴
>> ylabel('value'); % y 轴
>> legend('sin', 'cos'); % 标识函数图像名称和颜色图标
>> title('my plot'); % 图像大标题
>> % print -dpng 'myplot.png' % 输出图像
>> close
>> figure(1); plot(t, y1); % 新建一个绘图窗口绘制y1
>> figure(2); plot(t, y2); % 新建一个绘图窗口绘制y2
>> subplot(1, 2, 1); % 绘制 1 行 2 列的图像
>> plot(t, y1);
>> subplot(1, 2, 2);
>> plot(t, y2);
>> axis([0.5 1 -1 1]); % 改变坐标轴取值范围
clf; 清空图像
>> A = magic(5);
>> imagesc(A);
>> imagesc(A), colorbar, colormap gray;
hist() 画直方图
条件和循环:
for i = 1 : 10,
v(i) = 2 ^ i;
end;
indices = 1 : 10;
for i = indices,
disp(i);
end;
i = 1;
while i <= 15,
i = i + 1;
if i == 6,
break;
elseif i == 7
continue;
else
disp(i);
end;
end;
函数:
文件函数 squareThisNumber.m
function y = squareThisNumber(x)
y = x ^ 2;
调用
>> squareThisNumber(5)
ans = 25
addpath('C\Users\Administrator\Desktop\'); 添加库目录
文件函数squareAndCubeThisNumber(x)
function [y1, y2] = squareAndCudeThisNumber(x)
y1 = x ^ 2;
y2 = x ^ 3;
矩阵和向量的转化:
aVec = [A1(:); A2(:); A3(:)];
A1 = reshape(aVec(1, 12), 3, 4);
A2 = reshape(aVec(11, 24), 3, 4);
A3 = reshape(aVec(25, 28), 1, 4);