通过在每次迭代中省略一个索引来生成一组索引的循环

Loop that generates an array of indices by omitting one index at each iteration

使用 for 循环,我将如何编写代码来生成索引数组,在循环的每次迭代 k 中,我将从 [1, 2, 3, ... N] 生成索引数组] 从集合中排除 k?

例如,如果我有 k = 3 次迭代,第一次迭代会给我索引 (2,3),第二次迭代会给我索引 (1,3),最后第三次迭代会给我索引 (1,2).

使用

all_indices = [1 2 3]; %// these can be arbitrary numbers, not necessarily 1:N
N = numel(all_indices);
for n = 1:N
    selected_indices = all_indices([1:n-1 n+1:N]);
end

如果你想一次生成所有行,作为单个矩阵的行,你可以使用nchoosek:

all_indices = [1 2 3]; %// again, these can be arbitrary numbers
selected_indices = nchoosek(all_indices, numel(all_indices)-1); %// generate combinations
selected_indices = flipud(selected_indices); %// put results in the intended order

在示例中,这给出了

selected_indices =
     2     3
     1     3
     1     2

方法 #1

您可以在每次迭代时使用setdiff排除当前迭代ID,就像这样-

for iteration_id = 1:3
    indices = setdiff(1:3,iteration_id)
end

代码运行-

indices =
     2     3
indices =
     1     3
indices =
     1     2

方法 #2(矢量化)

您可以采用矢量化方法一次性生成所有索引,如果您必须使用这些索引,可以很容易地在循环内使用 -

num_iters = 3; %// Number of iterations

all_indices = repmat([1:num_iters]',1,num_iters) %//'
all_indices(1:num_iters+1:end)=[]
valid_indices = reshape(all_indices,num_iters-1,[])'

代码运行-

valid_indices =
     2     3
     1     3
     1     2

另一种非常简单的方法:

N=3;
for k=1:N
    [1:k-1,k+1:N]
end

任何n

的另一种方式
 n = 3;
 [ii, ~] = find( ~eye(n) );
 indices = reshape( ii, n-1, [] ).'

如果您不关心列的顺序,您可以简单地使用:

n = 5;
indices = reshape(ones(n-1,1)*(n:-1:1),n,[]);

不完全是最不言自明的,但它滥用了矩阵的结构indices

indices = 5     4     3     2
          5     4     3     1
          5     4     2     1
          5     3     2     1
          4     3     2     1