octave相较于MATLAB是有开源免费的的优势。
首先,octave命令直接进入环境
加减乘除和其他语言等同理
% 是注释
1 ~= 2 是表示1不等于2,这个和其他语言不同
xor(1,0) 是表示异或
PS1('>>'); 表示换命令提示符。其中单引号中的内容可换成自己习惯用的任意符号。
octave:1> PS1('~~');
~~PS1('>>');
>>a = 3 % 把3赋值给a
a = 3
>>a = 3; % 加分号会阻止输出
>>b = 'abc'; % 字符串赋值
>>b % 输出b
b = abc
>>c = (3>=4); % c赋为假值
>>c
c = 0
>>a = pi; % 圆周率π
>>a
a = 3.1416
>>disp(a); % 特殊输出
3.1416
octave:2> disp(sprintf(' 2 decimals: %0.2f', a)) % 自定义格式输出
2 decimals: 3.14
octave:3> disp(sprintf(' 6 decimals: %0.6f', a)) % 六位小数输出
6 decimals: 3.141593
octave:4> format long % 长型输出
octave:5> a
a = 3.14159265358979
octave:6> format short % 短型输出
octave:7> a
a = 3.1416
octave:1> A = [1 2; 3 4; 5 6] %矩阵赋值
A =
1 2
3 4
5 6
octave:1> A = [1 2; % 另一种定义矩阵的方式
> 3 4;
> 5 6]
A =
1 2
3 4
5 6
octave:2> V = [1 2 3] % 定义一个行向量或3*1矩阵
V =
1 2 3
octave:3> V = [1; 2; 3] % 定义一个列向量或1*3矩阵
V =
1
2
3
octave:4> V = 1:0.1:2 % 定义一个行向量从1开始到2,间距为0.1(1*11矩阵)
V =
Columns 1 through 8:
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000
Columns 9 through 11:
1.8000 1.9000 2.0000
octave:1> V = 1:6 % 定义一个行向量或6*1矩阵,octave默认间距为1
V =
1 2 3 4 5 6
octave:2> ones(2,3) % 生成一个2*3矩阵,默认所有元素为1
ans =
1 1 1
1 1 1
octave:3> C = 2*ones(2,3) % 生成一个2*3矩阵,设置所有元素为2
C =
2 2 2
2 2 2
octave:4> C = [2 2 2; 2 2 2]
C =
2 2 2
2 2 2
octave:5> W = zeros(1,3)
W =
0 0 0
octave:6> W = rand(1,3) % 生成随机矩阵或向量(所有项的值介于0-1)
W =
0.24500 0.59770 0.99636
octave:1> rand(3,3) % 生成随机矩阵或向量(所有项的值介于0-1)
ans =
0.74228 0.20606 0.61900
0.33074 0.43885 0.29686
0.89678 0.10433 0.73303
octave:2> W = randn(1,3) % 服从高斯分布(均值为0,标准差或者方差为1)
W =
0.36618 0.67564 -1.44800
octave:5> W = -6 + sqrt(10)*(randn(1,10)) % 复杂的
W =
Columns 1 through 8:
-3.4220 -7.7356 -1.9949 -10.4508 -5.2208 -5.6821 -6.6212 -6.5277
Columns 9 and 10:
-7.6240 -10.2428
octave:5> hist(w) % 绘制直方图
octave:6> hist(w,50)
octave:1> eye(4) % 生成4*4的单位矩阵
ans =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
octave:2> I =eye(4)
I =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
octave:5> help eye % 显示单位矩阵的帮助函数,q键退出
'eye' is a built-in function from the file libinterp/corefcn/data.cc
-- Built-in Function: eye (N)
-- Built-in Function: eye (M, N)
-- Built-in Function: eye ([M N])
-- Built-in Function: eye (..., CLASS)
Return an identity matrix.
If invoked with a single scalar argument N, return a square NxN
identity matrix.
If supplied two scalar arguments (M, N), 'eye' takes them to be the
number of rows and columns. If given a vector with two elements,
'eye' uses the values of the elements as the number of rows and
columns, respectively. For example:
eye (3)
=> 1 0 0
0 1 0
0 0 1
The following expressions all produce the same result:
eye (2)
==
eye (2, 2)
==
eye (size ([1, 2; 3, 4]))
The optional argument CLASS, allows 'eye' to return an array of the
specified type, like
val = zeros (n,m, "uint8")
root@iZwz90nfcu1g26zyib2289Z:~# help help % 显示help命令的帮助文档
help: help [-dms] [pattern ...]
Display information about builtin commands.
Displays brief summaries of builtin commands. If PATTERN is
specified, gives detailed help on all commands matching PATTERN,
otherwise the list of help topics is printed.
Options:
-d output short description for each topic
-m display usage in pseudo-manpage format
-s output only a short usage synopsis for each topic matching
PATTERN
Arguments:
PATTERN Pattern specifiying a help topic
Exit Status:
Returns success unless PATTERN is not found or an invalid option is given.
版权声明:本文为zhangyu4863原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。