I have a matlab file (.m) and I want to run this file using python. I do not have matlab on my ubuntu system. Can I still run the matlab file?
isomonotone.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);
Can I run this file using python without having matlab on my ubuntu?
-
...no? However, see this question for some Matlab->Python cross-compilers. Also, why do you need to run this Matlab code, when you already got a Python translation of it in your previous question?senshin– senshin2014年01月04日 06:46:55 +00:00Commented Jan 4, 2014 at 6:46
-
the thing is, in my prev question was for understanding how to convert to python code. I have bunch of matlab files. I will convert all the files to python but I need to check if the output of python files same as matlab files. thats the reason why I wanted to run matlab files as wellsam– sam2014年01月04日 07:31:51 +00:00Commented Jan 4, 2014 at 7:31
3 Answers 3
You can install Octave from the Ubuntu repository (or download and install the latest - that's more work)
Starting Octave in the directory where this file is, allows me to run the %! tests, thus:
octave:1> test ismonotone
PASSES 4 out of 4 tests
In fact the presence of those %! suggests that this file was originally written for Octave. Can someone confirm whether MATLAB can handle those doctest like lines?
edit - add interactive examples
octave:1> ismonotone(linspace(0,1,20))
ans = 1
octave:2> ismonotone(zeros(1,100))
ans = 3
Or from the linux shell
1424:~/myml$ octave -fq --eval 'ismonotone(linspace(0,1,20))'
ans = 1
For someone used to running Python scripts from the shell, Octave is more friendly than MATLAB. The startup overhead is much smaller, and the command line options are more familiar. In the interactive mode, doc opens a familiar UNIX info system.
2 Comments
Have you tried: 1. Small Matlab to Python compiler 2. LiberMate 3. Rewrite the code using SciPy module.
Hope this helps
Comments
Python cannot run Matlab programs natively. You would need to rewrite this in Python, likely using the SciPy libraries.