Convert python numpy 2D array to matlab 2D array

조회 수: 45 (최근 30일)
Manju gurik
Manju gurik 2022년 2월 22일
댓글: Manju gurik 2022년 2월 24일
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

답변 (1개)

Sean de Wolski
Sean de Wolski 2022년 2월 23일
편집: Sean de Wolski 2022년 2월 23일
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
Sean de Wolski
Sean de Wolski 2022년 2월 24일
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
Manju gurik 2022년 2월 24일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by