Skip to content

Commit

Permalink
add numpy to matlab array converter
Browse files Browse the repository at this point in the history
  • Loading branch information
maedoc committed Sep 18, 2015
1 parent 4f82f57 commit 063d776
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion conda.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ function conda(cmd, varargin)

persistent conda_path conda_env conda_py


switch cmd
% TODO this prevents true conda install from working.
case 'install'
install(varargin{:});
case 'use'
Expand Down
62 changes: 62 additions & 0 deletions np2m.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function y = np2m(x)
% Convert NumPy array to MATLAB array

% kudos to http://fr.mathworks.com/matlabcentral/answers/157347-convert-python-numpy-array-to-double#answer_157736

%% setup table of conversions from Python array type codes to MATLAB classes
% is persistent faster than setting up the table every time?
persistent t2m % type code to matlab class

if isempty(t2m)
t2m.c = @char;
t2m.b = @int8;
t2m.B = @uint8;
t2m.h = @int16;
t2m.H = @int16;
t2m.i = @int32;
t2m.I = @uint32;
t2m.l = @int64;
t2m.L = @uint64;
t2m.f = @single;
t2m.d = @double;
end

%% handle type of array
type_code = char(x.dtype.char);
matlab_class = t2m.(type_code);

%% convert data
y = matlab_class(py.array.array(type_code, py.numpy.nditer(x)));

%% handle shape if required
ndim = x.ndim * 1;
if ndim > 1
shape = int32(zeros([1 ndim]));
for i=1:ndim
shape(i) = x.shape{ndim+1-i}*1;
end
y = reshape(y, shape);
end

%% benchmarks
% Running for various sizes, we can see it's not zero-copy, so to be used
% with this in mind.

%{
[np2m] 0.08 MB, 0.04 ms / iter / KB
[np2m] 0.76 MB, 0.03 ms / iter / KB
[np2m] 7.63 MB, 0.03 ms / iter / KB
%}

if 0
%%
n = 1000000;
m = 100;
x = py.numpy.random.randn(n).reshape([10, -1]);
tic
for i=1:m
y = np2m(x);
end
fprintf('[np2m] %0.2f MB, %.2f ms / iter / KB\n', ...
n*8/1024/1024, toc*1000/m/(n*8/1024));
end

0 comments on commit 063d776

Please sign in to comment.