Python 如何处理foor循环中动态变化大小的数组

Python 如何处理foor循环中动态变化大小的数组,python,matlab,Python,Matlab,在我的代码中,有一个矩阵是动态增加大小的。Matlab中的伪代码如下所示: cnt = 0 for ii = 1:M for jj = 1:N if (condition satisfied) cnt = cnt + 1 A(cnt, :, :) = I # I is a matrix that is created within the loop end end end 如何使用NumPy在Pyt

在我的代码中,有一个矩阵是动态增加大小的。Matlab中的伪代码如下所示:

cnt = 0
for ii = 1:M
    for jj  = 1:N
        if (condition satisfied)
           cnt = cnt + 1
           A(cnt, :, :) = I # I is a matrix that is created within the loop
        end
    end
 end

如何使用NumPy在Python中实现这一点?

A by np.array需要进一步重塑以获得该结果equivalent@jingweimo我不这么认为。@TheBlackCat:代码可以工作,只是我需要进一步重塑生成的数组。矩阵不能动态调整大小。它们假装是,但MATLAB正在创建一个新数组,并通过循环将所有数据复制到新数组中。这就是为什么MATLAB编辑器会警告您不要在循环中调整数组的大小。
import numpy as np

A = list()
for i in range(M):
    for j in range(N):
        if condition satisfied:
            A.append(I)    # I is a ndarray created within the loop.

A = np.array(A)