Convert python numpy 2D array to matlab 2D array
Show older comments
Hi,
i am calling python function which returns numpy 2D array in this fashion.
data = [ [10, 20, 30, 40], [100, 200], [10, 40, 50, 80, 90, [10, 00, 88, 99, 199, 100]]
when i try converting into matlab array
mat_array = double(data)
its giving me error
Error using py.numpy.ndarray/double
Conversion of Python 'ndarray' type to MATLAB 'double' is only supported for real numbers and logicals.
But when i have 1D numpy array
data_1 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500]
double(data_1)
This works.
Can you please guide me on converting python numpy 2D array to matlab array
Answers (1)
Sean de Wolski
on 23 Feb 2022
Edited: Sean de Wolski
on 23 Feb 2022
What is the underlying python class of your ndarray? It doesn't seem to be recognized by double. Here, I create it with int8 and that works with double and int8 in MATLAB. Look at the display to see the hint or post back with more details
>> nd = py.numpy.zeros_like(int8(magic(4)))
nd =
Python ndarray:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Use details function to view the properties of the Python object.
Use int8 function to convert to a MATLAB array.
>> double(nd)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> int8(nd)
ans =
4×4 int8 matrix
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
11 Comments
Stephen23
on 23 Feb 2022
Manju gurik's incorrectly posted "Answer" moved here:
Hi Sean,
Thank you for the reply.
My data is python double numpy array of array, its jagged array having different columns for each row, here is the data
[[-613.0, -2386.0, 571.0, 2391.0, -609.0, -2387.0, 605.0, 2391.0, -620.0, -2386.0, 587.0, 2391.0, -616.0, -2387.0, 586.0, 2390.0]
[336.0, -38.0, -356.0, 39.0, 393.0, -36.0, -460.0, 40.0, 411.0, -36.0, -432.0, 39.0, 483.0, -35.0, -425.0, 39.0, 485.0, -38.0, -532.0, 39.0, 491.0, -35.0, -472.0, 40.0, 450.0, -36.0, -386.0, 39.0, 402.0, -36.0, -383.0, 38.0, 238.0, -35.0, -303.0, 39.0, 251.0 ]
[1101.0, -3048.0, -1136.0, 3054.0, 1141.0, -3048.0, -1159.0]
[974.0, -1457.0, -936.0, 1460.0, 843.0, -1457.0, -716.0, 1460.0, 625.0, -1457.0, -609.0, 1461.0, 608.0, -1457.0, -631.0, 1461.0, 790.0, -1457.0, -798.0]
]
details(inputCurve.curve_data)
py.numpy.ndarray handle with properties:
T: [1×1 py.numpy.ndarray]
base: [1×1 py.NoneType]
ctypes: [1×1 py.numpy.core._internal._ctypes]
data: [1×150 py.memoryview]
dtype: [1×1 py.numpy.dtype]
flags: [1×1 py.numpy.flagsobj]
flat: [1×1 py.numpy.flatiter]
imag: [1×1 py.numpy.ndarray]
itemsize: [1×1 py.int]
nbytes: [1×1 py.int]
ndim: [1×1 py.int]
real: [1×1 py.numpy.ndarray]
shape: [1×1 py.tuple]
size: [1×1 py.int]
strides: [1×1 py.tuple]
Can you please help, how do i convert python numpy Jagged array to matab jagged array.
Thank you.
@Manju gurik: you can create a "jagged array" using a cell array of vectors.
Manju gurik
on 23 Feb 2022
Edited: Manju gurik
on 23 Feb 2022
Sean de Wolski
on 23 Feb 2022
MATLAB can't represent jagged as a double. It either needs to be an Nx1 cell of 1xM vectors or you can pad out the jaggedness with NaN.
Can you give me the code to create the curve_data array from MATLAB?
Manju gurik
on 23 Feb 2022
Sean de Wolski
on 23 Feb 2022
>> d = py.cdata.get_curve_data
d =
Python ndarray:
[array([1., 2.]) array([11. , 22.2, 33.3, 44.4, 55.5, 66. ])
array([23.2, 33.2, 43.3])]
>> lc = py.list(d)
lc =
Python list with no properties.
[array([1., 2.]), array([11. , 22.2, 33.3, 44.4, 55.5, 66. ]), array([23.2, 33.2, 43.3])]
>> c = cellfun(@double, cell(lc), 'UniformOutput', false)
c =
1×3 cell array
{[1 2]} {[11 22.2000 33.3000 44.4000 55.5000 66]} {[23.2000 33.2000 43.3000]}
Manju gurik
on 23 Feb 2022
Edited: Manju gurik
on 23 Feb 2022
Sean de Wolski
on 24 Feb 2022
Probably not. The fastest approach in both languages will always be to use primitive types. Thus the suggestion of NaN-padding out the jaggedness to make a flat 2d array that can be directly converted or a 1d vector with NaNs separating the curve segments (like what the Mapping Toolbox does for geographic data).
Manju gurik
on 24 Feb 2022
Sean de Wolski
on 24 Feb 2022
Using gRPC through python or .NET seems perfectly logical to me. You'll just need a little data massaging after getting it back. I think I'd recommend going to the NaN-delimited segments approach, e.g. getting the ndarray you have into:
[1.2 2.3 nan 3.4 23 2.3 nan 1 1 1 1 1]
Manju gurik
on 24 Feb 2022
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!