1

My data is stored in classes in MATLAB which contain large arrays but want to work with that data in python for training machine learning. From what I can tell, when moving data between the two, the value is transferred and so the array is copied in memory. Then converting the arrays into numpy arrays creates a further copy in memory. How can I avoid creating so copies of data as much as possible.

I have not yet implemented the python code as it will be pointless if I can't transfer it easily.

1 Answer 1

3

There are 3rd-party solutions which share memory between MATLAB/Python or native approaches as documented:

https://uk.mathworks.com/help/matlab/matlab_external/passing-data-to-python.html

Pass Matrices and Multidimensional Arrays to Python

The Python language provides a protocol for accessing memory buffers like the data stored in a MATLAB array. MATLAB implements this Python buffer protocol for MATLAB arrays so that you can read MATLAB arrays directly from Python code, running in the same process as MATLAB, without copying data.

1
  • 1
    Note that np.asarray(a) will create a NumPy array without copying the data. This is not mentioned in the linked MATLAB documentation. Commented Apr 24, 2024 at 13:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.