Mac、Windows、Ubuntu(Linux)如何下载安装Octave

Mac、Windows、Ubuntu(Linux)如何下载安装Octave

Octave是什么?

Octave是一款免费的开源软件,或者说Octave是一门科学编程语言。Octave有强大的数学导向语法,内置绘图和可视化工具,可以运行在GNU / Linux,macOS,BSD和Windows上,与Matlab脚本兼容。我们讲一下它的安装,以及它的使用,包括一些基本的命令和语法。

Octave官网

Octave安装

下载安装地址:https://www.gnu.org/software/octave ,根据自己的系统,选择喜欢的方式安装。

Octave安装

其中Mac可以通过homebrew安装,命令如下:

brew install octave

也可以下载安装包安装:Mac版Octave安装包,根据自己的系统版本下载。

初识Octave

我在命令行中使用brew安装了Octave,然后我输入octave 打开Octave。我们先看一些简单的语法例子:

基本的数学运算

octave:1> 5+6
ans =  11
octave:2> 3-2
ans =  1
octave:3> 5*8
ans =  40
octave:4> 1/2
ans =  0.50000
octave:5> 2^6
ans =  64

emmm...看起来还不错哦

逻辑运算

首先说明一下,Octave中,% 并不是我们平时用的高级语言的除法取余操作,而是注释的意思。而条件判断中 ~= 表示不等于。

octave:6> 1 == 2   % false
ans = 0
octave:7> 1 ~= 2   % true
ans = 1
octave:8> 1 && 0   % AND
ans = 0
octave:9> 1 || 0   % OR
ans = 1
octave:10> xor(1,0)
ans = 1

打印与赋值

像是这样:

octave:17> a = 3
a =  3
octave:18> b = 2;
octave:19> b
b =  2

加上 ; 只是赋值,不打印。除了直接输入变量名打印,也可以用disp(),就像这样:

octave:20> a = pi;
octave:21> disp(a)
 3.1416

也有类似c语言的操作:

octave:22> disp(sprintf('%0.2f', a))
3.14

也可以像这样控制输出长度:

octave:24> a
a =  3.1416
octave:25> format long
octave:26> a
a =  3.141592653589793
octave:27> format short
octave:28> a
a =  3.1416

矩阵和向量

我们学习机器学习可能会比较关心矩阵和向量的这一部分,那我们来创建个矩阵和向量玩一下

octave:29> A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

octave:30> A = [1 2;
> 3 4;
> 5 6]
A =

   1   2
   3   4
   5   6

octave:31> v = [1 2 3]
v =

   1   2   3

octave:32> v = [1; 2; 3]
v =

   1
   2
   3

还有更好玩的,通过下面的方式,我们可以创建1到2,步长0.1的行向量,或者说是1行11列的矩阵:

octave:33> v = 1:0.1:2
v =

    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000    1.8000    1.9000    2.0000

当然,他的退化版本,就是去掉步长,像这样:

octave:34> v = 1:6
v =

   1   2   3   4   5   6

再来看一下其他生成方法:

ones()生成的每个元素都是1

octave:35> ones(2,3)
ans =

   1   1   1
   1   1   1

octave:36> C = 2*ones(2,3)
C =

   2   2   2
   2   2   2

zeros()生成的每个元素都是0

octave:37> w = zeros(1,3)
w =

   0   0   0

eye() 是用来生成单位矩阵,例如我们生成一个4x4的单位阵:

octave:45> eye(4)
ans =

Diagonal Matrix

   1   0   0   0
   0   1   0   0
   0   0   1   0
   0   0   0   1

rand()随机生成,每次都不同哦

octave:38> w = rand(1,3)
w =

   0.22440   0.77716   0.89388

octave:39> rand(3,3)
ans =

   0.525231   0.025986   0.013181
   0.035461   0.237802   0.322027
   0.487019   0.078959   0.126589

octave:40> rand(3,3)
ans =

   0.338512   0.816598   0.045129
   0.380178   0.882227   0.869696
   0.873298   0.842133   0.611917

randn()高斯分布

octave:41> w = randn(1,3)
w =

   1.051191   0.749710  -0.046791

再稍微高级一点,我们画个图玩玩:

octave:42> w = -6 + sqrt(10)*(randn(1,10000));
octave:43> hist(w)

octave绘制直方图

octave:44> hist(w,50)

octave绘制直方图

帮助命令

这里只介绍了一些简单的基础的命令和语法,具体的我们后面会单独介绍。在这之前,先讲一个help命令,它就相当于Linux中的man,你有任何不清楚的,你都可以help一下,比如:

help rand

当然,你也可以

help help

结果就像这样:

octave:46> help help
'help' is a function from the file /usr/local/Cellar/octave/5.1.0_2/share/octave/5.1.0/m/help/help.m

 -- help NAME
 -- help --list
 -- help .
 -- help
     Display the help text for NAME.

     For example, the command 'help help' prints a short message
     describing the 'help' command.

     Given the single argument '--list', list all operators, keywords,
     built-in functions, and loadable functions available in the current
     session of Octave.

     Given the single argument '.', list all operators available in the
     current session of Octave.

     If invoked without any arguments, 'help' displays instructions on
     how to access help from the command line.

     The help command can provide information about most operators, but
     NAME must be enclosed by single or double quotes to prevent the
     Octave interpreter from acting on NAME.  For example, 'help "+"'
     displays help on the addition operator.

     See also: doc, lookfor, which, info.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.

总结

本节主要讲了Octave的下载安装,以及一些基础的使用。要多练习,掌握octave的基本使用。另外,Octave本身就是模仿matlab的,语法也是近乎相同,掌握二者之一,另一个就可以不用学也能玩了。

有的小伙伴用R或者Python或者其他高级语言学习机器学习,这样是不推荐的,因为那样增加了学习成本,很多算法再octave或者matlab可以一行或者几行实现,这是其他一些语言不能比的。实际生产中也是一样,我们可以先尝试用octave或者matlab去实现,测试没问题之后,再转成其他语言,这样相对来说会减少时间成本。

猜你喜欢
Octave命令及语法教程3 - 图像绘制与数据可视化
阅读 4062

当开发学习算法时 图像可以让你更好地理解算法的内容 检查算法是否正常运行 是否达到了算法的目的 例如绘制损失函数J(θ)可以帮助确认梯度下降算法是否收敛 Octave有非常简单的工具用来生成大量不同的图

什么是机器学习
阅读 3269

机器学习,简单地说就是拟人。人工智能包含机器学习,而机器学习又包含深度学习。机器学习在我们的生活中有很多的应用,像自动驾驶、搜索引擎、谷歌翻译、垃圾邮件识别等等。

Octave命令及语法教程2 - 数据的计算(矩阵乘法、求逆、转置)
阅读 4866

上次我们学会了在Octave中如何加载或存储数据、如何把数据存入矩阵,等等。这次我们谈谈Octave中如何对数据进行运算,包括矩阵乘法、求逆矩阵、求矩阵转置,以及一些求和、求最大值等基础函数。

Octave命令及语法教程1 - 数据的导入、存储、移动
阅读 5307

这节课的内容 能让你明白 在 Octave 中 怎样用几句简单的命令 很快地对数据进行移动 包括加载和储存一个向量 或矩阵 加载和存储数据 把矩阵放在一起 构建更大的矩阵 用索引对矩阵某个特定元素进行操作等等

Mac如何安装MATLAB破解版
阅读 6540

MATLAB是matrix和laboratory的组合,意为矩阵实验室,是一款特别好用的数学软件,尤其是对机器学习、人工智能。今天介绍一下Mac下安装MATLAB破解版教程。据说Mac上的matlab更丝滑。