📜  Octave中的基本操作

📅  最后修改于: 2022-05-13 01:54:54.197000             🧑  作者: Mango

Octave中的基本操作

GNU Octave 是一种高级编程语言,主要用于数值计算。它还可以用于轻松实现各种机器学习算法。 Octave 是开源的,即它可以免费使用,而 MATLAB 不是,因此 MATLAB 需要许可证才能运行。

以下是 Octave 的各种基本功能:

1.算术运算:八度可用于执行基本的数学运算,如加法,减法,乘法,幂运算等。

MATLAB
% addition operation
23 + 65 + 8
 
% subtraction operation
32 - 74
 
% power operation
6 ^ 2
 
% multiplication operation
45 * 7
 
% division operation
5 / 6


MATLAB
% logical AND    
1 && 0
 
% logical OR
1 || 0
 
% logical NOT
~1


MATLAB
% equal to
1 == 1
 
% not equal to
0 ~= 0
 
% greater than   
1 > 0
 
% less than
1 < 0
 
% greater than equal to 
1 >= 2
 
% less than equal to
0 <= 0


MATLAB
PS1('<< ');
PS1('@ ');
PS1('# ');


MATLAB
% variable declaration and initialization
var = 2
 
% if we want to create the variable and don't want to print it
% then put a semicolon at the end of that command
var = 3; % this time the variable will not be printed
 
% variable of datatype char
ch = 'c'
 
% storing the result of an operation in a variable
res = (1 != 1)
 
% storing the value of pi in a variable
var = pi
 
% printing a variable with disp() function
disp(var);
 
% using sprintf() function to print a string
disp(sprintf('3 decimal values : %0.3f', var))
 
% using format long to resize
format long
var
 
% using format short to resize
format short
var


MATLAB
% creating matrix in row major
matrix = [1 2 3; 4 5 6; 7 8 9]


MATLAB
% creating row vector
r_v = [1, 2, 3]
 
% creating column vector
c_v = [1; 2; 3]


MATLAB
% creating vector using ":"
% the extreme end values denote the range
% and the middle value denotes the step
v1 = 1 : 5 : 20
v2 = 1 : 0.5 : 5
 
% without the step parameter
v3 = 1 : 10
 
% generate matrix of size 4x4 with all element as 1
ones_matrix = ones(4, 4)
 
% generate matrix of size 4x4 with all element as 10
M = 10 * ones(4, 4)
 
% generate row vector of size 5 with all elements 0
zeroes_vector = zeros(1, 5)
 
% generate row vector of some random numbers between 0 and 1
random_vector = rand(1, 5)
 
% generate matrix of some random numbers between 0 and 1
random_matrix = rand(3, 4)
 
% generate matrix with Gaussian distribution
% where mean = 0 and variance and standard deviation = 1
gauss_matrix = randn(5, 5)
 
% generate identity matrix with size 5x5
identity_matrix = eye(5)


MATLAB
% generate a vector with 1000 elements
elements_1000 = 1 + sqrt(25)*(randn(1, 1000));
 
hist(elements_1000 )


MATLAB
% generate a vector with 1000 elements
elements_1000 = 1 + sqrt(25)*(randn(1, 1000));
 
% histogram with 30 bins
hist(elements_1000, 30)


MATLAB
help eye
help sqrt
help hist


输出 :

ans = 96
ans = -42
ans = 36
ans = 315
ans = 0.83333

2. 逻辑运算: Octave 可用于执行 AND、OR、NOT 等逻辑运算。

MATLAB

% logical AND    
1 && 0
 
% logical OR
1 || 0
 
% logical NOT
~1

输出 :

ans = 0
ans = 1
ans = 0

3. 关系运算: Octave 可用于执行关系运算,例如大于、小于等。

MATLAB

% equal to
1 == 1
 
% not equal to
0 ~= 0
 
% greater than   
1 > 0
 
% less than
1 < 0
 
% greater than equal to 
1 >= 2
 
% less than equal to
0 <= 0

输出 :

ans = 1
ans = 0
ans = 1
ans = 0
ans = 0
ans = 1

4. 更改默认八度提示符号:默认八度提示符号为“>>”。我们可以使用以下命令更改默认的 Octave 提示符号:

MATLAB

PS1('<< ');
PS1('@ ');
PS1('# ');

输出 :

5. 变量:与其他编程语言一样,Octave 也有变量来临时存储数据。

MATLAB

% variable declaration and initialization
var = 2
 
% if we want to create the variable and don't want to print it
% then put a semicolon at the end of that command
var = 3; % this time the variable will not be printed
 
% variable of datatype char
ch = 'c'
 
% storing the result of an operation in a variable
res = (1 != 1)
 
% storing the value of pi in a variable
var = pi
 
% printing a variable with disp() function
disp(var);
 
% using sprintf() function to print a string
disp(sprintf('3 decimal values : %0.3f', var))
 
% using format long to resize
format long
var
 
% using format short to resize
format short
var

输出 :

var =  2
ch = c
res = 0
var =  3.1416
 3.1416
3 decimal values : 3.142
var =  3.141592653589793
var =  3.1416

6. 矩阵和向量:现在让我们学习如何在 Octave 中处理矩阵和向量。我们可以创建矩阵,如下所示。

MATLAB

% creating matrix in row major
matrix = [1 2 3; 4 5 6; 7 8 9]

输出 :

matrix =

   1   2   3
   4   5   6
   7   8   9

我们也可以做一个向量,一个向量是一个n行1列的矩阵(列向量)或1行n列的矩阵(行向量)。在示例 2 和 3 中,中间值 5 和 0.5 表明我们想要创建一个从 1 到 20 的向量矩阵,跳跃为 5,从 0 到 5 的跳跃分别为 0.5。

MATLAB

% creating row vector
r_v = [1, 2, 3]
 
% creating column vector
c_v = [1; 2; 3]

输出 :

r_v =

   1   2   3

c_v =

   1
   2
   3

以下是一些创建矩阵和向量的实用程序快捷方式:

MATLAB

% creating vector using ":"
% the extreme end values denote the range
% and the middle value denotes the step
v1 = 1 : 5 : 20
v2 = 1 : 0.5 : 5
 
% without the step parameter
v3 = 1 : 10
 
% generate matrix of size 4x4 with all element as 1
ones_matrix = ones(4, 4)
 
% generate matrix of size 4x4 with all element as 10
M = 10 * ones(4, 4)
 
% generate row vector of size 5 with all elements 0
zeroes_vector = zeros(1, 5)
 
% generate row vector of some random numbers between 0 and 1
random_vector = rand(1, 5)
 
% generate matrix of some random numbers between 0 and 1
random_matrix = rand(3, 4)
 
% generate matrix with Gaussian distribution
% where mean = 0 and variance and standard deviation = 1
gauss_matrix = randn(5, 5)
 
% generate identity matrix with size 5x5
identity_matrix = eye(5)

输出 :

v1 =

    1    6   11   16

v2 =

    1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000    4.5000    5.0000

v3 =

    1    2    3    4    5    6    7    8    9   10

ones_matrix =

   1   1   1   1
   1   1   1   1
   1   1   1   1
   1   1   1   1

M =

   10   10   10   10
   10   10   10   10
   10   10   10   10
   10   10   10   10

zeroes_vector =

   0   0   0   0   0

random_vector =

   0.79085   0.35395   0.92267   0.60234   0.75549

random_matrix =

   0.64434   0.67677   0.54105   0.83149
   0.70150   0.16149   0.38742   0.90442
   0.60075   0.82273   0.37113   0.91496

gauss_matrix =

   0.705921   1.336101  -0.097530   0.498245   1.125928
  -0.550047  -1.868716  -0.977788   0.319715  -0.603599
  -0.018352  -2.133200   0.462272   0.169707   1.733255
   0.623343   0.338734   0.618943   1.110172   1.731495
  -1.741052  -0.463446   0.556348   1.633956  -1.424136

identity_matrix =

Diagonal Matrix

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

7.直方图:我们可以绘制直方图hist()函数。我们还可以更改直方图的桶大小或箱。

MATLAB

% generate a vector with 1000 elements
elements_1000 = 1 + sqrt(25)*(randn(1, 1000));
 
hist(elements_1000 )

输出 :


MATLAB

% generate a vector with 1000 elements
elements_1000 = 1 + sqrt(25)*(randn(1, 1000));
 
% histogram with 30 bins
hist(elements_1000, 30)

输出 :

8. 帮助:我们可以使用帮助命令查看任何函数的文档。

MATLAB

help eye
help sqrt
help hist

输出 :