使用随机生成的数组填充数组时的索引错误

时间:2014-08-11 16:32:35

标签: matlab

运行这段关于索引的代码时出现错误。我已经多次运行逻辑并且还没有发现我的错误,我认为这是我编写本节的方式。任何帮助将不胜感激。如果我遗漏了对这段代码至关重要的任何信息,请告诉我。

index_pairs = [1,12661;12662,46147;46148,52362]    
group_class_count = [10137,2524;127448,20738;1570,4645]
group_count = 3
cross_sections = 10

for j=1:group_count
    rand_index=randsample(index_pairs(j,1):index_pairs(j,2),(group_class_count(j,1)+group_class_count(j,2)),true);  % Creates an index of random rows for the current group.
    cross_size(j)=floor(size(rand_index,2)/cross_sections);
    for k=1:cross_sections
        cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};
    end
 end

error: Index exceeds matrix dimensions. Error in cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))}; 

1 个答案:

答案 0 :(得分:1)

如果你改变了

cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};

cross_rand_indices(j,k)={rand_index(cross_size(j)*(k-1)+1:cross_size(j)*(k))};

错误将消失。
我认为这符合您在外部循环中将某些内容保存到cross_size(j)时的意图。