使用python运行matlab.m文件

2024-09-30 08:21:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个matlab文件(.m),我想用python运行这个文件。我的ubuntu系统上没有matlab。我还能运行matlab文件吗?在

等单调.m

% Checks vector v for monotonicity, and returns the direction (increasing,  
% decreasing, constant, or none).
%
% Meaning of parameter tol:
% - if tol==0, check for non-increasing or non-decreasing sequence (default).
% - if tol>0, allow backward steps of size <= tol
% - if tol<0, require forward steps of size >= tol
%
% Inputs
%  v:   vector to check for monotonicity
%  tol: see above
% 
% Outputs
%  b: a bitfield indicating monotonicity.  Can be tested as follows:
%   bitand(b,1)==true  -->  v is increasing (within tolerance)
%   bitand(b,2)==true  -->  v is decreasing (within tolerance)
%   bitand(b,3)==true  -->  v is both increasing and decreasing
%                           (i.e. v is constant, within tolerance).
% --------------------------------------------------------------------------
function b = ismonotone( v, tol )
  if ( nargin < 2 )
    tol = 0;
  end

  b = 0;
  dv = diff(v);
  if ( min(dv) >= -tol ) b = bitor( b, 1 ); end
  if ( max(dv) <= tol ) b = bitor( b, 2 ); end
end


%!test assert(ismonotone(linspace(0,1,20)),1);
%!test assert(ismonotone(linspace(1,0,20)),2);
%!test assert(ismonotone(zeros(1,100)),3);
%!test
%! v=[0 -0.01 0 0 0.01 0.25 1];
%! assert(ismonotone(v,0.011),1);
%! assert(ismonotone(-v,0.011),2);

我能用python运行这个文件而不在ubuntu上运行matlab吗?在


Tags: 文件oftestforifisassertend
3条回答

Python不能以本机方式运行Matlab程序。您需要在Python中重写这个,很可能使用SciPy库。在

您可以从Ubuntu存储库安装Octave(或者下载并安装最新版本—这需要更多的工作)

在该文件所在的目录中启动Octave,允许我运行%!测试,因此:

octave:1> test ismonotone
PASSES 4 out of 4 tests

事实上,%!的存在表明这个文件最初是为Octave编写的。有人能确认MATLAB是否能处理那些doctest类的行吗?在

编辑-添加交互式示例

^{pr2}$

或者从linux shell

1424:~/myml$ octave -fq  eval 'ismonotone(linspace(0,1,20))'
ans =  1

对于习惯于从shell运行Python脚本的人来说,Octave比MATLAB更友好。启动开销要小得多,并且命令行选项更为熟悉。在交互模式下,doc打开一个熟悉的UNIX信息系统。在

您是否尝试过: 1Small Matlab to Python compiler 2LiberMate 三。使用SciPy模块重写代码。在

希望这有帮助

相关问题 更多 >

    热门问题