在我的代码中,有一个矩阵可以动态增加大小。 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中实现它?
答案 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)