简体   繁体   English

如何在for循环中添加不同大小的numpy数组条目(类似于Matlab的单元格数组)?

[英]How to add to numpy array entries of different size in a for loop (similar to Matlab's cell arrays)?

I'm trying to implement an analouge to Matlab's cell array in python \\ numpy. 我正在尝试在python \\ numpy中对Matlab的单元数组实现一个analouge。 The Matlab code would go like that : Matlab代码就是这样的:

for n=1:10
    C{n} = rand(1,n);
end

Note that each cell element has a different length. 注意,每个单元元素具有不同的长度。 Now in numpy: 现在在numpy:

for n in np.arange(10):
    C[n] = np.random.rand(1,n)

and I get an error, what can I do to implement this? 我得到一个错误,我该怎么做才能实现这个?

In most simple cases, you could just use a standard Python list. 在大多数简单的情况下,您可以使用标准的Python列表。 They are pretty similar to Matlab cell-arrays, you can use them to store anything: 它们非常类似于Matlab单元格数组,您可以使用它们来存储任何内容:

C = []
for n in np.arange(10):
    C.append(np.random.rand(1,n))

This would be a good option if the list is not too long and if it only has a single dimension (so just a vector). 如果列表不是太长并且它只有一个维度(所以只是一个向量),这将是一个很好的选择。 Note that in Python, you typically do not pre-allocate a list of the final size, but simply append to an empty list. 请注意,在Python中,您通常不会预先分配最终大小的列表,而只是附加到空列表。 Python lists are optimized for appending to the end, they already do some sort of pre-allocation under the hood. Python列表经过优化以便追加到最后,他们已经在底层进行了某种预分配。

If you are translating Matlab code with cell-arrays of multiple dimensions, or ones that are very large, you could use numpy arrays with dtype=object : 如果要将Matlab代码与多维度的单元格数组或非常大的单元格数组进行转换,则可以使用带有dtype=object numpy数组:

m, n = 3, 6
C = np.empty((m, n), dtype=object)
for i in xrange(m):
    for j in xrange(n):
        C[i, j] = np.random.rand(i, j)

You are using a list there. 你在那里使用一个列表。 So, before indexing into it with [n] inside that loop, you need to initialize that list. 因此,在使用该循环内的[n]索引到它之前,您需要初始化该列表。 One of the ways to initialize a list is like this - 初始化列表的方法之一是这样的 -

C = [None]*10 # Here 10 is the length of the list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Numpy相当于MATLAB的单元阵列 - Numpy equivalent of MATLAB's cell array numpy数组内部具有不同大小的数组 - Numpy array with arrays of different size inside 如何为两个不同大小的 numpy arrays 列表中的类似列表获取 Boolean 矩阵 - How to get Boolean matrix for similar lists in two different-size numpy arrays of lists 检查numpy数组中有多少numpy数组等于另一个不同大小的numpy数组中的其他numpy数组 - Check how many numpy array within a numpy array are equal to other numpy arrays within another numpy array of different size Python / Numpy:如何分配数组的结束+ 1元素,类似于在Matlab中完成的方式? - Python/Numpy: How do you assign the end+1 element of an array similar to how it's done in Matlab? 如何有效地处理类似于 Matlab 的 blkproc(blockproc)函数的块中的 numpy 数组 - How can I efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function 为什么numpy允许你添加不同大小的数组? - Why does numpy let you add different size arrays? 将数组添加到numpy数组 - Add arrays to the numpy array 如何将大小为 1 的 numpy 数组插入到空的 numpy 数组中? - How to insert numpy arrays of size 1 into an empty numpy array? 乘以不同大小的numpy数组 - multiplying numpy arrays of different size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM