如何在foor循环中处理一系列dyanically改变的大小

时间:2017-06-03 02:48:28

标签: 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在Python中实现它?

1 个答案:

答案 0 :(得分:1)

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)