machine learning

Octave常用指令笔记

2017-09-26  本文已影响20人  edwin1993

四则:

图片.png

逻辑运算

图片.png

变量相关

图片.png

在拥有GUI的Octave中,所有变量在左侧栏中有所显示:

图片.png

矩阵相关

图片.png 图片.png 图片.png 图片.png 图片.png 图片.png 图片.png 图片.png

路径相关的操作

类似于linux,不在此详细说明

图片.png

数据加载

通过ones(500,2)生成了一个500*2的矩阵,随后存储起来,并clear了变量。

图片.png

通过load加载刚才存储的ans并打印出来,发现ans与刚刚生成的内容一致。


图片.png

矩阵计算

初始:


图片.png

" .运算符"
“.”在这里起到的作用是表明计算为对应位置的计算,如A * B是不能相乘的因为他们都是 3 * 2的矩阵,但是.*使得他们对应位置相乘。.^2则是对应位置取平方。

图片.png

1./为取倒数

图片.png

幂运算
e^n 次方

图片.png

绝对值

图片.png

转置

图片.png

取最大值
值得一提的是,因为A为矩阵,所以max(A)获得的是每列最大的值

图片.png
max(A)是下面这个写法的缩写 图片.png

找到大于某值的元素
计数序列从左到右,按列计数。
1表示第一列的第一个,2表示第一列的第二个...

图片.png

和、积、取整等

图片.png 图片.png

矩阵化为向量

图片.png

求逆矩阵

图片.png

画图

t = [0:0.01:0.98]
y1 = sin(2*pi*4*t)
plot(t,y1)
图片.png

要使新图在旧图的基础上进行绘制,则需要使用hold on;

plot(t,y1)
y2 = cos(2*pi*4*t);
hold on
plot(t,y2,'r')
xlabel('time')
ylabel('value')
legend('sin','cos')
title('my plot')
图片.png

可以使用指令分别打开两个图

figure(1); plot(t,y1)
figure(2); plot(t,y2)
图片.png

也可以在同一个图中进行分割
subplot(1,2,1)中前面的两个数表示分割为1*2的块,而1表示现在使用第一块。

subplot(1,2,1)
plot(t,y1)
subplot(1,2,2)
plot(t,y2)
axis([0.5 1 0 1])

axis([0.5 1 0 1])将改变右图的坐标轴,x轴为0.5-1,y轴为

图片.png
A = magic(5)
imagesc (A)
colorbar

图中不同颜色表示不同的数值

图片.png

控制关键字

图片.png

函数

创建了一个函数,函数名为squareThisNumber.m


图片.png

在octave中调用:


图片.png
函数必须以.m结尾,否则就会出现“未定义”错误。
另外,返回值支持多返回值:
function [y1 y2] = squareAndCubeThisNumber(x)

y1 = x^2
y2 = x^3
图片.png

也支持多个变量输入

function J = costFunction(X,y,theta)

% X is the 'design matrix' play the role of the training examples
% y is the class labels of X

m = size(X,1); % get the number of examples
prediction = X * theta;
sqrErrors = (prediction - y).^2;

J = 1/(2*m)*sum(sqrErrors);
上一篇下一篇

猜你喜欢

热点阅读