Index higher dimensions in MATLAB Engine API for Python
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Using the MATLAB Engine API in Python, I am only able to index the first dimension of a two- or more-dimensional MATLAB array. Is there any way to index a higher dimension?
import matlab.engine
import numpy as np
arr = matlab.int32([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
Here, arr is a small (4x3) sample array, though in practice I, have much larger arrays of more than two dimensions. Now if I want to take just the third column, I must first convert it to a numpy array, as in the following:
subset = np.array(arr)[:,2]
print(subset)
This returns [3, 6, 9, 12], which is what I want. But when I have much larger arrays, converting the entire MATLAB array to a numpy array is very slow, and all I want is a small slice of it. Is there any way to do this using the MATLAB API for Python? As it is now, I can subset one or more rows, but I cannot subset columns.
6 Kommentare
Adam Wasserman
am 8 Aug. 2020
Bearbeitet: Adam Wasserman
am 8 Aug. 2020
Unfortunately, this is actually an issue of Python lists, not MATLAB arrays. MATLAB arrays are treated as lists in Python, not numpy arrays. That means that a 2D "array" is actually a list of lists, a 3D "array" a list of lists of lists, and so on. Thus, slicing will not work in the way you'd expect for a numpy array.
There may be some workarounds, but I'm not super familiar with working with multidimensional lists. This may help: http://ilan.schnell-web.net/prog/slicing/
Antworten (0)
Siehe auch
Kategorien
Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!