Convert python numpy 2D array to matlab 2D array

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)

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

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.
Hi Stephen,
can you please provide me some example to convert numpy array of array(jagged array) to matlab cell array.
Here is my numpy array returned from Python code
curve.curve_data
ans =
Python ndarray with properties:
T: [1×1 py.numpy.ndarray]
base: [1×1 py.NoneType]
ctypes: [1×1 py.numpy.core._internal._ctypes]
data: [1×3 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]
[[-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, -607.0, -2386.0, 599.0, 2391.0, -616.0, -2386.0, 590.0, 2391.0, -607.0, -2387.0, 589.0, 2391.0, -632.0, -2387.0, 571.0, 2390.0, -602.0, -2387.0, 577.0, 2391.0, -602.0, -2386.0, 599.0, 2391.0, -629.0, -2386.0, 584.0, 2391.0, -636.0, -2387.0, 587.0, 2390.0, -624.0, -2386.0, 579.0, 2390.0, -651.0, -2387.0, 560.0, 2390.0, -606.0, -2387.0, 592.0, 2391.0, -621.0, -2386.0, 583.0, 2391.0, -583.0, -2386.0, 558.0, 2390.0, -578.0, -2387.0, 575.0, 2391.0, -606.0, -2387.0, 592.0, 2390.0, -610.0, -2387.0, 595.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, -36.0, -224.0, 40.0, 126.0, -38.0, -170.0, 39.0, 93.0, -36.0, -73.0, 39.0, 97.0, -36.0, -82.0, 38.0, 51.0, -36.0, -8.0, 39.0, 39.0, -36.0, -73.0, 39.0, 125.0, -38.0, -113.0, 39.0, 101.0, -38.0, -126.0, 39.0, 215.0, -36.0, -240.0, 39.0, 258.0, -36.0, -285.0, 39.0, 380.0, -36.0, -416.0, 39.0]
[1101.0, -3048.0, -1136.0, 3054.0, 1141.0, -3048.0, -1159.0, 3053.0, 1137.0, -3048.0, -1110.0, 3054.0, 1140.0, -3049.0, -1137.0, 3053.0, 1148.0, -3047.0, -1143.0, 3054.0, 1141.0, -3047.0, -1118.0, 3054.0, 1142.0, -3048.0, -1065.0, 3054.0, 1120.0, -3047.0, -1121.0, 3053.0, 1098.0, -3048.0, -1123.0, 3054.0, 1170.0, -3048.0, -1190.0, 3053.0, 1115.0, -3048.0, -1153.0, 3054.0, 1118.0, -3048.0, -1140.0, 3054.0, 1172.0, -3047.0, -1122.0, 3053.0, 1124.0, -3048.0, -1192.0, 3054.0, 1103.0, -3048.0, -1118.0, 3053.0, 1138.0, -3048.0, -1089.0, 3054.0]]
when i do, double(curve.curve_data), i get this error
double(curve.curve_data)
Error using py.numpy.ndarray/double
Conversion of Python 'ndarray' type to MATLAB 'double' is only supported for real numbers and logicals.
Thank you.
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?
Hi Sean,
Here my python module test.py
def get_curve_data():
curve_data = np.array([])
list_data = [[1.0, 2.0], [11.0, 22.2, 33.3, 44.4, 55.5, 66.], [23.2, 33.2, 43.3]]
np_ar = [np.array(x) for x in list_data]
curve_data = np.append(curve_data, np_ar)
return curve_data
In matlab function, we call
py_curve_data = py.test.get_curve_data()
data = double(py_curve_data)
gives error
Error using py.numpy.ndarray/double
Conversion of Python 'ndarray' type to MATLAB 'double' is only supported for real numbers and logicals.
>> 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]}
Hi Sean,
Thank you for the solution. This works, but its slow compared to directly converting numpy array to matlab double array, it takes long time to convert the data. Is there any way to speed up this.
Its same as sending python of list of list data from python module function instead of numpy array.
For larger data, it even takes more than a minute, almost 2 minutes to convert to matlab array.
Earlier, for uniform 2D array, its was taking 1 second to convert matlab array.
Thank you,
Regards
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).
Hi Sean,
Thank you for the response. Yes, i agree. We will use this approach.
We are using python to connect to grpc client since we couldnt find any grpc matlab client generation code.
Do we have one matlab grpc stub and service generator plugin.
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]
Ok. Got it. Thank you.
I understand that we dont have one available for matlab directly to connect to grpc services without Python or .NET.

Sign in to comment.

Asked:

on 22 Feb 2022

Commented:

on 24 Feb 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!