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

2024-03-28 09:20:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中,有一个矩阵是动态增加大小。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中实现这一点?你知道吗


Tags: 代码forifthatis动态矩阵condition
1条回答
网友
1楼 · 发布于 2024-03-28 09:20:56
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)

相关问题 更多 >