首页 > 其他分享 >octave函数

octave函数

时间:2023-06-05 16:44:42浏览次数:45  
标签:magic 函数 ## length octave mul

  在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 [email protected]
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

 

标签:magic,函数,##,length,octave,mul
From: https://www.cnblogs.com/guochaoxxl/p/17458171.html

相关文章

  • main(...) 函数
                                              《目录》      最小的main()函数形参返回值获取main()函数的返回值argcAND argv最小的main()函数intmain(){}//默认返回......
  • 2022-2023 春学期 矩阵与数值分析 C6 插值函数的应用
    2022-2023春学期矩阵与数值分析C6插值函数的应用原文C6插值函数的应用6.1插值型求积公式数值型求积公式用于解决难以找到原函数的积分问题问题描述:设\(f(x)\)是定义在\([a,b]\)上的可积函数,考虑带权积分\[I(f)=\int^b_a\rho(x)f(x)dx\]其中权函数\(\rho(x)\)......
  • Java集合List转树结构工具类-函数版
    工具类:packagecom.example.mindsa.util.tree;importcom.baomidou.mybatisplus.core.toolkit.support.SFunction;importlombok.SneakyThrows;importjava.lang.invoke.SerializedLambda;importjava.lang.reflect.Method;importjava.util.List;importjava.util.func......
  • PE学习——导出表,加载dll并GetProcAddress获取函数地址的内在原理
    导出表一个可执行程序是由多个PE文件组成,这些PE文件依靠倒入表、导出表进行联系,导出表存储着PE文件提供给其他人使用的函数列表,导入表则存储着PE文件所需要用到的PE文件列表。从PE文件的角度去看,任何PE文件都可以有导入、导出表,从一般情况下来看,EXE文件不会提供导出表,也就是不会......
  • 类内构造函数前缀explicit
    只有一个参数的构造函数前面加上explicit,这样一来在创建对象时不会被转换类型,因调用构造函数时有explicit限制,如classMyClass{public:explicitMyClass(intvalue):data(value){}intgetData()const{returndata;}private:intdat......
  • Flask之钩子函数
    Flask之钩子函数类似django的中间件,作用就是在进入框架的之后http方法之前或返回response之前进行一些操作Flask的钩子函数可在注册时根据注册的app或者蓝图从而确定钩子函数作用的范围(可全局也可作用某一个蓝图)钩子函数钩子函数可以分为两层说明,第一层是 app 层,第二层则......
  • C++ 多态 虚函数virtual
    先解释虚函数,对于基类,子类继承基类后可能会调用其某个函数FA,而不同的子类继承了同一个基类后需要基类内某个同样的函数FA但又不是同个作用,此时则会在对应的子类内对应重载派生出FA_B函数和FA_C函数,而这时要求FA为虚函数(virtual)那为什么不各自写成一个函数B和C呢?这就是多态的意......
  • Linux下三组I/O复用函数的比较(select、poll、epoll)
        前面我们讨论了select、poll和epoll三组I/O复用系统调用,这三组系统调用都能同时监听多个文件描述符。它们将等待由timeout参数指定的超时时间,直到一个或多个文件描述符上有事件发生时返回,返回值是就绪的文件描述符的数量。返回0表示没有事件发生。现在我们从事件集、最......
  • Excel批量插入图片(Excel函数集团)
    批量插入图片,归函数集团管了?对,你没看错,就是函数集团的活!因为Microsoft365出了一个新函数:IMAGE!所以,以前折腾的那种一堆合并以后再贴进txt文本文件再贴回来的,没用了?是与不是,我们用实例来说话! ***一条不算太华丽的分割线***准备工作1:一堆图片图片还是那个图片,但保存的位置却不是那个......
  • c++函数调用压栈过程
    c++函数调用,栈内情况如下图所示:首先主函数将被调函数所需参数从右至左压入栈中然后再将主函数地址即返回地址EIP压入栈中再将主函数栈基址EBP压入栈中,此时构造被调函数栈,将当前ESP值mov给EBP,即被调函数栈从此处开始上图ida反汇编代码,可以看到对变量的使用,参数(argc,argv,env......