Index higher dimensions in MATLAB Engine API for Python
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam Wasserman
2020-8-8
编辑:Adam Wasserman
2020-8-8
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/
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!