Python 跟踪最优指标,同时每次迭代将矩阵减少1x1

Python 跟踪最优指标,同时每次迭代将矩阵减少1x1,python,arrays,numpy,iteration,matrix-indexing,Python,Arrays,Numpy,Iteration,Matrix Indexing,设置 我有一个NxN对角矩阵,在每次迭代中我将矩阵收缩1x1 indices = np.arange(0, np.size(n_n_matrix, 0)).tolist() for iter in range(0, N-K) opt_indices = criterion(n_n_matrix) lost_index = [i for i in indices if i not in opt_indices][0] n_n_matrix = np.delete(traj_

设置

我有一个NxN对角矩阵,在每次迭代中我将矩阵收缩1x1

indices = np.arange(0, np.size(n_n_matrix, 0)).tolist()
for iter in range(0, N-K)
    opt_indices = criterion(n_n_matrix)
    lost_index = [i for i in indices if i not in opt_indices][0]
    n_n_matrix = np.delete(traj_dist_matrix, lost_index, axis=0)
    n_n_matrix = np.delete(traj_dist_matrix, lost_index, axis=1)
直到得到KxK对角矩阵。 我如何根据未删除索引在原始NxN矩阵中的位置跟踪它们

失败

我失败了,因为我尝试了:

lost_indices = [], list_indices_iter = []
>>>loop above<<<
    count_1 = sum(lost_index >= idx for idx in lost_indices_iter)
    count_2 = sum(lost_index + count_1 >= idx for idx in lost_indices_iter) - count_1
    ...
    lost_indices.append(lost_index + count_1 + count_2 ...)
    lost_indices_iter.append(lost_index)
left_opt_indices = [i for i in indices if i not in lost_indices]
缩小问题范围


下面的例子说明了我的问题:如果我删除索引I,下一个矩阵将收缩。如果我在下一次迭代中删除索引j>=I,我需要将1添加到j中,因为它的位置与原始NxN矩阵相比减少了。如果我随后删除索引k=>j,我需要考虑之前的两个更改,依此类推。

也许不是最好的方法,但我会跟踪字典,其中键是原始索引位置,值是当前索引位置。我称之为索引映射

我创建了一个函数,用于将删除应用于索引映射,并显示您可以创建一个删除列表并在最后应用所有删除,但也可以逐个应用它们

每个删除事件都位于x或y位置以及当前数组的索引中

def process_deletions(index_mapping, deletions):
    for deletion in deletions:
        del_kind = deletion['kind']
        del_ind = deletion['index']

        for orig_ind,curr_ind in index_mapping[del_kind].items():
            #Don't update an index if it's less than the one deleted
            if curr_ind < del_ind:
                continue

            #Set the deleted index to now be -1
            elif curr_ind == del_ind:
                index_mapping[del_kind][orig_ind] = -1

            #Otherwise if the index is greater than the one deleted
            #decrement the new index it points to
            #i.e. if index 3 is deleted, what was index 5 is now index 4
            else:
                index_mapping[del_kind][orig_ind] -= 1

    return index_mapping


num_rows = 10
num_cols = 10

#Start out that each original index points to it's same position in the unmodified matrix
index_mapping = {
    'x':{i:i for i in range(num_rows)},
    'y':{i:i for i in range(num_cols)},
}

#Keep track of all the deletions made
#Can alternatively make one deletion at a time and keep applying it to the index_mapping
deletions = [
    {'kind':'x', 'index':1},
    {'kind':'y', 'index':1},
    {'kind':'x', 'index':6},
    {'kind':'y', 'index':6},
    {'kind':'x', 'index':4},
    {'kind':'y', 'index':4},
]
index_mapping = process_deletions(index_mapping, deletions)

print(index_mapping)
更新:我的一个朋友给了我一个提示。这是一艘两轮班轮

tracker_original_indices = np.arange(0, np.size(traj_dist_matrix, 0))
tracker_original_indices = np.delete(tracker_original_indices, lost_index, axis=0)

非常感谢。你的解决方案有效。然而,我找到了一个更简单的方法,见上图!