octave函数
在octave中有很多的函数应用,例如:命令type magic查看magic函数的源码
1 magic is the user-defined function defined from: /usr/share/octave/6.4.0/m/special-matrix/magic.m 2 3 ######################################################################## 4 ## 5 ## Copyright (C) 1999-2021 The Octave Project Developers 6 ## 7 ## See the file COPYRIGHT.md in the top-level directory of this 8 ## distribution or <https://octave.org/copyright/>. 9 ## 10 ## This file is part of Octave. 11 ## 12 ## Octave is free software: you can redistribute it and/or modify it 13 ## under the terms of the GNU General Public License as published by 14 ## the Free Software Foundation, either version 3 of the License, or 15 ## (at your option) any later version. 16 ## 17 ## Octave is distributed in the hope that it will be useful, but 18 ## WITHOUT ANY WARRANTY; without even the implied warranty of 19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 ## GNU General Public License for more details. 21 ## 22 ## You should have received a copy of the GNU General Public License 23 ## along with Octave; see the file COPYING. If not, see 24 ## <https://www.gnu.org/licenses/>. 25 ## 26 ######################################################################## 27 28 ## -*- texinfo -*- 29 ## @deftypefn {} {} magic (@var{n}) 30 ## 31 ## Create an @var{n}-by-@var{n} magic square. 32 ## 33 ## A magic square is an arrangement of the integers @code{1:n^2} such that the 34 ## row sums, column sums, and diagonal sums are all equal to the same value. 35 ## 36 ## Note: @var{n} must be a scalar greater than or equal to 3. If you supply 37 ## @var{n} less than 3, magic returns either a nonmagic square, or else the 38 ## degenerate magic squares 1 and []. 39 ## @end deftypefn 40 41 function A = magic (n) 42 43 if (nargin != 1) 44 print_usage (); 45 endif 46 47 n = fix (n); 48 if (n < 0) 49 error ("magic: N must be non-negative"); 50 elseif (n < 1) 51 A = []; 52 elseif (mod (n, 2) == 1) 53 54 shift = floor ((0:n*n-1)/n); 55 c = mod ([1:n*n] - shift + (n-3)/2, n); 56 r = mod ([n*n:-1:1] + 2*shift, n); 57 A(c*n+r+1) = 1:n*n; 58 A = reshape (A, n, n); 59 60 elseif (mod (n, 4) == 0) 61 62 A = reshape (1:n*n, n, n)'; 63 I = [1:4:n, 4:4:n]; 64 J = fliplr (I); 65 A(I,I) = A(J,J); 66 I = [2:4:n, 3:4:n]; 67 J = fliplr (I); 68 A(I,I) = A(J,J); 69 70 elseif (mod (n, 4) == 2) 71 72 m = n/2; 73 A = magic (m); 74 A = [A, A+2*m*m; A+3*m*m, A+m*m]; 75 k = (m-1)/2; 76 if (k > 1) 77 I = 1:m; 78 J = [2:k, n-k+2:n]; 79 A([I,I+m],J) = A([I+m,I],J); 80 endif 81 I = [1:k, k+2:m]; 82 A([I,I+m],1) = A([I+m,I],1); 83 I = k + 1; 84 A([I,I+m],I) = A([I+m,I],I); 85 86 endif 87 88 endfunction 89 90 91 %!test 92 %! for i = 3:30 93 %! A = magic (i); 94 %! assert (norm(diff([sum(diag(A)),sum(diag(flipud(A))),sum(A),sum(A')])),0); 95 %! endfor 96 97 ## Not a magic square but we must return something (bug #46672). 98 ## While one day we may change the actual return of magic (2), 99 ## this properties still must be true. 100 %!test <*46672> 101 %! m = magic (2); 102 %! assert (size (m), [2 2]); 103 %! assert (m, [4 3; 1 2]); 104 105 %!assert (isempty (magic (0))) 106 %!assert (magic (1), 1) 107 %!assert (magic (1.5), 1) 108 109 ## Test input validation 110 %!error magic () 111 %!error magic (1, 2) 112 %!error <N must be non-negative> magic (-5)
使用也特别容易,magic(3)
magic(3) ans = 8 1 6 3 5 7 4 9 2
查看帮助:
man magic error: 'man' undefined near line 1, column 1 octave:9> help magic warning: tempdir: '/tmp/' does not exist or is not a directory warning: called from tempdir at line 48 column 5 __makeinfo__ at line 135 column 17 help at line 109 column 24 'magic' is a function from the file /usr/share/octave/6.4.0/m/special-matrix/magic.m -- magic (N) Create an N-by-N magic square. A magic square is an arrangement of the integers '1:n^2' such that the row sums, column sums, and diagonal sums are all equal to the same value. Note: N must be a scalar greater than or equal to 3. If you supply N less than 3, magic returns either a nonmagic square, or else the degenerate magic squares 1 and []. 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.
通过系统函数magic的源码明显看出m函数有几个部分:
1、函数定义: function [返回变量列表(多个变量之间用逗号分割)] = 函数名称(言简意赅,顾名思义即可)(输入变量列表(多变量之间用逗号隔开))
2、帮助文本:第1行叫做H1行帮助文本,有特殊作用,经常用来说明函数的功能,版权等信息
3、函数主题:函数体语句段,%后边的是注释语句
4、函数最后的end语句,可以省略
说明:函数定义中function关键字、函数名称和参数列表的括号,其他都可以省略,其中函数名可以自己按照标识符的要求自己编写,其他都是不能乱改的。
对每一个函数,m环境自动生成两个变量nargin和nargout实现变量检测,无需声明可以直接使用
子函数一般位于主函数中,常常位于非第一个函数的位置,可以被主函数或其他子函数调用
主函数一般位于函数的第一个位置,函数名称于文件名称一般同名
1 %剔除向量a的奇异数据,系统的测量精度为可设置cal 2 function remainP = distriMethodV2(a, cal) 3 b = sort(a); %向量数据排序 xm = getXm(b); %获取向量的中位>值 fl = b(1:ceil(length(b)/2)); 4 if rem(length(b), 2) == 0 5 fl = [fl xm]; 6 end 7 fll = getXm(fl); 8 9 if rem(length(b), 2) ~= 0 10 fu = b(ceil(length(b)/2) : length(b)); 11 else 12 fu = [xm, b((length(b)/2 + 1) : length(b))]; 13 end 14 fuu = getXm(fu); 15 16 remainP = b(find(abs(b) <= cal*(fuu - fll))); 17 18 %求向量中位值 function xm = getXm(b) 19 if rem(length(b), 2) ~= 0 20 index = ceil(length(b)/2); 21 xm = b(index); 22 else 23 index = length(b)/2; 24 xm = (b(index) + b(index + 1))/2; 25 end
function mul = multiV2(a) mul = 1; for i = 1 : length(a) mul = mul * a(i); end
也可以:
function mul = multi(varargin) mul = 1; for i = 1 : length(varargin) mul = mul * varargin{i}; end
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。