Convert python numpy array to double
Show older comments
The plot call below will throw an error because x is not a matlab type. How do you convert a python numpy array to a regular matlab matrix?
x = py.numpy.random.random([4,4]);
plot(x)
Sincerely, Peter
3 Comments
Geoff Hayes
on 4 Oct 2014
Peter - you must be using the new functionality from R2014b which allows for easier to interface with Python. In this example, what does x return?
Peter
on 4 Oct 2014
Peter
on 4 Oct 2014
Accepted Answer
More Answers (3)
Jim Hokanson
on 4 Nov 2014
Here's another approach. This is alluded to by http://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-python.html
data = double(py.array.array('d',py.numpy.nditer(x))); %d is for double, see link below on types
data = reshape(data,[4 4])'; %Could incorporate x.shape here ...
py.array.array is apparently the way Matlab suggests getting data from Python into Matlab (see link above) https://docs.python.org/2/library/array.html
This however requires an iterable over which the array can be constructed, hence the call to nditer()
Once the value is an array, then Matlab has written functionality for casting to a Matlab type, in this case via double.
6 Comments
Peter
on 5 Nov 2014
Jim, this is a beautiful solution. And it's quick too. Thank you so much for the code and for your explanation, Peter.
Daniel Golden
on 7 May 2015
Minor modifications to make the code more generalizable, including some you suggested in your comments:
data_row = double(py.array.array('d', py.numpy.nditer(x, pyargs('order', 'F')))); % Add order='F' to get data in column-major order (as in Fortran 'F' and Matlab
data_size = cell2mat(cell(x.shape));
data = reshape(data, data_size); % No need for transpose, since we're retrieving the data in column major order
David Laurenson
on 15 Jun 2016
Edited: David Laurenson
on 16 Jun 2016
I love this solution! I had to modify it slightly (I'm not sure if this is a version issue or not), as cell2mat didn't work properly for me as cell() returned a cell array of py.int, not integers... Then I got to addressing the issue of multidimensional arrays, and realised that things were getting more complicated. As a result, I've written two functions - mat2nparray and nparray2mat - that will convert between the two formats:
function result = mat2nparray( matarray )
%mat2nparray Convert a Matlab array into an nparray
% Convert an n-dimensional Matlab array into an equivalent nparray
data_size=size(matarray);
if length(data_size)==1
% 1-D vectors are trivial
result=py.numpy.array(matarray);
elseif length(data_size)==2
% A transpose operation is required either in Matlab, or in Python due
% to the difference between row major and column major ordering
transpose=matarray';
% Pass the array to Python as a vector, and then reshape to the correct
% size
result=py.numpy.reshape(transpose(:)', data_size);
else
% For an n-dimensional array, transpose the first two dimensions to
% sort the storage ordering issue
transpose=permute(matarray,[2 1 3:length(data_size)]);
% Pass it to python, and then reshape to the python style of matrix
% sizing
result=py.numpy.reshape(transpose(:)', fliplr(size(transpose)));
end
end
and
function result = nparray2mat( nparray )
%nparray2mat Convert an nparray from numpy to a Matlab array
% Convert an n-dimensional nparray into an equivalent Matlab array
data_size = cellfun(@int64,cell(nparray.shape));
if length(data_size)==1
% This is a simple operation
result=double(py.array.array('d', py.numpy.nditer(nparray)));
elseif length(data_size)==2
% order='F' is used to get data in column-major order (as in Fortran
% 'F' and Matlab)
result=reshape(double(py.array.array('d', ...
py.numpy.nditer(nparray, pyargs('order', 'F')))), ...
data_size);
else
% For multidimensional arrays more manipulation is required
% First recover in python order (C contiguous order)
result=double(py.array.array('d', ...
py.numpy.nditer(nparray, pyargs('order', 'C'))));
% Switch the order of the dimensions (as Python views this in the
% opposite order to Matlab) and reshape to the corresponding C-like
% array
result=reshape(result,fliplr(data_size));
% Now transpose rows and columns of the 2D sub-arrays to arrive at the
% correct Matlab structuring
result=permute(result,[2 1 3:length(data_size)]);
end
end
Christoph Wiedemann
on 15 Mar 2017
Hey thanks for all these beautiful functions. I had to modify David's solution for multi-dimensional arrays. Here is the new code, embedded in a matlab class using static functions, and some test code:
classdef matpy
%MATPY Summary of this class goes here
% Detailed explanation goes here
properties
end
methods(Static)
function result = mat2nparray( matarray )
%mat2nparray Convert a Matlab array into an nparray
% Convert an n-dimensional Matlab array into an equivalent nparray
data_size=size(matarray);
if length(data_size)==1
% 1-D vectors are trivial
result=py.numpy.array(matarray);
elseif length(data_size)==2
% A transpose operation is required either in Matlab, or in Python due
% to the difference between row major and column major ordering
transpose=matarray';
% Pass the array to Python as a vector, and then reshape to the correct
% size
result=py.numpy.reshape(transpose(:)', int32(data_size));
else
% For an n-dimensional array, transpose the first two dimensions to
% sort the storage ordering issue
transpose=permute(matarray,[length(data_size):-1:1]);
% Pass it to python, and then reshape to the python style of matrix
% sizing
result=py.numpy.reshape(transpose(:)', int32(fliplr(size(transpose))));
end
end
function result = nparray2mat( nparray )
%nparray2mat Convert an nparray from numpy to a Matlab array
% Convert an n-dimensional nparray into an equivalent Matlab array
data_size = cellfun(@int64,cell(nparray.shape));
if length(data_size)==1
% This is a simple operation
result=double(py.array.array('d', py.numpy.nditer(nparray)));
elseif length(data_size)==2
% order='F' is used to get data in column-major order (as in Fortran
% 'F' and Matlab)
result=reshape(double(py.array.array('d', ...
py.numpy.nditer(nparray, pyargs('order', 'F')))), ...
data_size);
else
% For multidimensional arrays more manipulation is required
% First recover in python order (C contiguous order)
result=double(py.array.array('d', ...
py.numpy.nditer(nparray, pyargs('order', 'C'))));
% Switch the order of the dimensions (as Python views this in the
% opposite order to Matlab) and reshape to the corresponding C-like
% array
result=reshape(result,fliplr(data_size));
% Now transpose rows and columns of the 2D sub-arrays to arrive at the
% correct Matlab structuring
result=permute(result,[length(data_size):-1:1]);
end
end
function test()
A = 1:5;
Anp = matpy.mat2nparray(A);
sa = size(A);
sAnp = cellfun( @(x) double(x), cell(Anp.shape));
assert (all(sAnp == sa));
for i1=1:size(A,1)
for i2=1:size(A,2)
assert(A(i1,i2) == Anp.item(int32(i1-1), int32(i2-1)));
end
end
Anpm = matpy.nparray2mat(Anp);
assert(all(A == Anpm));
A = reshape(1:6, [2,3]);
Anp = matpy.mat2nparray(A);
sa = size(A);
sAnp = cellfun( @(x) double(x), cell(Anp.shape));
assert (all(sAnp == sa));
for i1=1:size(A,1)
for i2=1:size(A,2)
assert(A(i1,i2) == Anp.item(int32(i1-1), int32(i2-1)));
end
end
Anpm = matpy.nparray2mat(Anp);
assert(all(all(A == Anpm)));
A = reshape(1:(2*3*4), [2,3,4]);
Anp = matpy.mat2nparray(A);
sa = size(A);
sAnp = cellfun( @(x) double(x), cell(Anp.shape));
assert (all(sAnp == sa));
for i1=1:size(A,1)
for i2=1:size(A,2)
for i3=1:size(A,3)
display(sprintf('%d %d %d -> %f %f', i1,i2,i3, A(i1,i2,i3), Anp.item(int32(i1-1), int32(i2-1), int32(i3-1))));
assert (A(i1,i2,i3) == Anp.item(int32(i1-1), int32(i2-1), int32(i3-1)))
end
end
end
Anpm = matpy.nparray2mat(Anp);
assert(all(all(all(A == Anpm))));
end
end
end
Hi Christoph,
Thanks for the code! However, it doesn't support the conversion of scalar arrays:
Error using reshape
Size vector must have at least two elements.
Error in matpy.nparray2mat (line 55)
result=reshape(result,fliplr(data_size));
This can be solved by modifying the first `if` statement in nparray2mat to:
if any(numel(data_size) == [0,1])
Cheers!
Eric Cousineau
on 30 May 2017
To build further upon y'all's work, I've made a really rough prototype, using `matpy`, to get `numpy`-friendly proxy that allows more natural referencing, indexing / slicing, etc., via `subsref` and `subsassgn`:
Example code:
pyA = py.numpy.eye(3);
mlA = NumPyProxy(pyA);
mlA(:)
double(mlA(:))
mlA(3, 2)
sub = mlA([2, 1], 3)
double(sub) % 2017-05-30T01:09-04:00 - Presently a bug, wrong order
sub2 = mlA([1, 2], 3)
double(sub2)
mlA([2, 1], 3) = 100 * [1, 2]
mlA(:) = 5
Note that PyProxy also permits casting of >1-D arrays, using `subsref` tricks, whereas MATLAB R2016b presently does not permit passing 2-D matrices (at least, as far as I've tried using other people's examples).
This has only been tested / tinkered with in R2016b, and still needs refinement / robustification.
I will try to see if hacks like this aren't needed in future versions of MATLAB.
Thanks a ton for taking the time to post y'all's stuff!
Shaowu Pan
on 27 Sep 2017
Edited: per isakson
on 27 Sep 2017
Weird discussion...
def npArray2Matlab(x):
return matlab.double(x.tolist())
2 Comments
Laszlo Kormoczi
on 14 Nov 2018
Then how to use this in MATLAB?
timo kvamme
on 2 Jan 2019
Thank you, this worked
Jim Hokanson
on 4 Oct 2014
Edited: Jim Hokanson
on 4 Oct 2014
Here's a very ugly solution.
temp = cellfun(@cell,cell(x.tolist),'un',0);
data = cell2mat(vertcat(temp {:}));
A slightly cleaner solution:
data = typecast(uint8(char(x.flat.base.data)),'double');
data = reshape(data,[4 4])'; %Could incorporate x.shape here ...
Then of course:
plot(data)
Sorry I don't have time to explain how you would know that you need to do these things. I have to run!
Jim
4 Comments
Peter
on 6 Oct 2014
Thank you Jim for your answer!
The cellfun solution works. But the typecasting does not work all the times - it's very odd.
Peter
Jim Hokanson
on 6 Oct 2014
This assumes the data type is double. If it isn't then the typecasting won't work.
Jim, it's a bit complicated. For example, if I run:
for jj = 1:10
x = py.numpy.random.random([10,10]);
try
data = typecast(uint8(char(x.flat.base.data)),'double');
disp(numel(data))
catch me
warning(me.message)
end
end
I should get 100 doubles, but I end up with something very different:
Warning: The first input must contain a multiple of 8 elements to convert from uint8 (8 bits) to double (64 bits).
29.00
8.00
7.00
10.00
Warning: The first input must contain a multiple of 8 elements to convert from uint8 (8 bits) to double (64 bits).
24.00
20.00
Warning: The first input must contain a multiple of 8 elements to convert from uint8 (8 bits) to double (64 bits).
56.00
Do you have any idea why the typecasting is broken? I would guess that it's something to do with the conversion from the flat.base to char.
Peter.
Jim Hokanson
on 8 Oct 2014
Edited: Jim Hokanson
on 8 Oct 2014
Interesting. The problem occurs when there is a zero in the underlying raw buffer (not a zero value but a 0 byte). I'm not sure if this desired or if it is a bug.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!