MATLAB 元胞数组的 Numpy 等效项 [英] Numpy equivalent of MATLAB's cell array
问题描述
我想在 Numpy 中创建一个类似 MATLAB 的元胞数组.我怎样才能做到这一点?
I want to create a MATLAB-like cell array in Numpy. How can I accomplish this?
推荐答案
Matlab 元胞数组 与 Python 列表最相似,因为它们可以保存任何对象 - 但 scipy.io.loadmat 将它们导入为 numpy 对象数组 - 这是一个带有 dtype=object 的数组.
Matlab cell arrays are most similar to Python lists, since they can hold any object - but scipy.io.loadmat imports them as numpy object arrays - which is an array with dtype=object.
老实说,虽然您使用 Python 列表也很不错 - 如果您持有一般对象,您将失去 numpy 数组的几乎所有优点(它们旨在保存一系列值,每个值都取相同的数量内存).
To be honest though you are just as well off using Python lists - if you are holding general objects you will loose almost all of the advantages of numpy arrays (which are designed to hold a sequence of values which each take the same amount of memory).
这篇关于MATLAB 元胞数组的 Numpy 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!