1&&0 %AND 1 ||0 % OR octave:6> PS1('>> '); >> octave:1> 5+6 ans = 11 octave:2> 1==2 ans = 0 octave:3> 1==2 %false %XXX %为注释 ans = 0 octave:4> 1~=2 %表示1不等于2 ans = 1 octave:5> xor(1,0) %异或 ans = 1 octave:6> PS1('>> '); %令提示符更精简 >> a =3 a = 3 >> a=3;%抑制打印输出 >> a =3 a = 3 >> a=3; >> a a = 3 >> b ='hids'; >> b b = hids >> c = (3>=1); >> c c = 1 >> a =pi; >> a a = 3.1416 >> disp(a); 3.1416 >> disp(sprintf('2 decimals: %0.2f', a)) 2 decimals: 3.14 >> disp('0.6f',a) error: Invalid call to disp. Correct usage is: -- disp (X) -- STR = disp (X) 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. >> disp(sprintf('%0.6f',a)) 3.141593 >> format long >> a a = 3.141592653589793 >> format short >> a a = 3.1416 >>
>> A = [1 2; 3 4; 5 6] A = 1 2 3 4 5 6 >> A = [1 2; > 3 4; > 4 5] A = 1 2 3 4 4 5 >> V =[1 2 3] V = 1 2 3 >> V =[1;2;3] V = 1 2 3
>> 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 >> v = 1:6 v = 1 2 3 4 5 6 >> ones(2,3) ans = 1 1 1 1 1 1 >> C = 2*ones(2,3) C = 2 2 2 2 2 2 >> C = [ 2 2 2;2 2 2] C = 2 2 2 2 2 2 >> w = zeros(1,3) w = 0 0 0 >> w = ones(1,3) w = 1 1 1 >> w = rand(1,3) %生成一行三列矩阵,每个元素都是介于0-1之间的随机数 w = 0.42258 0.36123 0.84682 >> rand(3,3) ans = 0.6879706 0.0030582 0.5453437 0.0901260 0.8243611 0.3456819 0.8819444 0.2436181 0.0290983 >>
若随机变量X服从一个数学期望为μ、方差为σ^2的正态分布,记为N(μ,σ^2)。其概率密度函数为正态分布的期望值μ决定了其位置,其标准差σ决定了分布的幅度。当μ = 0,σ = 1时的正态分布是标准正态分布。 > >w = randn(1,3) 生成一行三列矩阵服从正态分布 w = -2.18891 1.25733 -0.92161 >> w = -6 + sqrt(10)*(randn(1, 10000)):生成一个随机的1x10000矩阵,元素值符合均值为-6,方差为10,标准差为根号10的高斯分布 hist(w) 绘制直方图
>> hist(w,50) %绘制50条的直方图
对角矩阵(diagonal matrix)是一个主对角线之外的元素皆为0的矩阵,常写为diag(a1,a2,...,an) 。
对角矩阵可以认为是矩阵中最简单的一种,值得一提的是:对角线上的元素可以为 0 或其他值,对角线上元素相等的对角矩阵称为数量矩阵;
对角线上元素全为1的对角矩阵称为单位矩阵。对角矩阵的运算包括和、差运算、数乘运算、同阶对角阵的乘积运算,且结果仍为对角阵 >> eye(6) ans = Diagonal Matrix 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 >>
>> eye(2,2) ans = Diagonal Matrix 1 0 0 1 >> eye (size ([1, 2; 3, 4])) ans = Diagonal Matrix 1 0 0 1 >> eye(2,3) ans = Diagonal Matrix 1 0 0 0 1 0 >> eye([2 3]) ans = Diagonal Matrix 1 0 0 0 1 0 >>
>> A = [1 2;3 4;5 6] A = 1 2 3 4 5 6 >> size(A) ans = 3 2 >> sz =size(A) sz = 3 2 >> size(sz) ans = 1 2 >> size(A,1) ans = 3 >> size(A,2) ans = 2 >> v = [1 2 3 4] v = 1 2 3 4 >> length(v) ans = 4 >> length(A) ans = 3 >> length([1;2;3;4;5]) ans = 5 >> pwd ans = C:Userscltt >> cd 'D:apps' >> pwd ans = D:apps >> cd ' C:Userscltt' error: C:Userscltt: Invalid argument >> cd 'C:Userscltt' >> pwd ans = C:Userscltt >> ls 驱动器 C 中的卷没有标签。 卷的序列号是 90A2-E5C6 C:Userscltt 的目录 [.] .octave_hist [Contacts] [Downloads] [Music] [Saved Games] [..] [.VirtualBox] [Desktop] [Favorites] [OneDrive] [Searches] [.config] [3D Objects] [Documents] [Links] [Pictures] [Videos] 1 个文件 278 字节 17 个目录 36,568,596,480 可用字节 >> load a.txt >> load('a.txt') >> who Variables in the current scope: A B C V a a_txt ans b c sz v w >> a a = 1 2 1 3 >> size(a) ans = 2 2
>> whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== A 3x2 48 double B 2x2 32 double C 2x3 48 double V 1x11 24 double a 2x2 32 double a_dat 4x2 64 double a_txt 3x2 48 double ans 1x2 16 double b 1x4 4 char c 1x1 1 logical sz 1x2 16 double v 1x4 32 double w 1x10000 80000 double Total is 10058 elements using 80365 bytes >> clear a_dat >> whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== A 3x2 48 double B 2x2 32 double C 2x3 48 double V 1x11 24 double a 2x2 32 double a_txt 3x2 48 double ans 1x2 16 double b 1x4 4 char c 1x1 1 logical sz 1x2 16 double v 1x4 32 double w 1x10000 80000 double Total is 10050 elements using 80301 bytes >> v = a(1:15)%把a.txt的前15个元素赋给v v = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
>> save b.txt v >> ls 驱动器 E 中的卷是 data 卷的序列号是 B662-58A9 E:dasandedasiokafterdeep learningmaterial 的目录 [.] [..] a.txt b.txt 2 个文件 917 字节 2 个目录 136,688,492,544 可用字节 >> clear%清除Oct的所有的变量 >> whos >> load b.txt >> b error: 'b' undefined near line 1 column 1 >> v v = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 >> whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== v 1x15 120 double Total is 15 elements using 120 bytes
>> save b.txt v -ascii
help 命令 >> help rand 'rand' is a built-in function from the file libinterp/corefcn/rand.cc -- rand (N) -- rand (M, N, ...) -- rand ([M N ...]) -- V = rand ("state") -- rand ("state", V) -- rand ("state", "re