简体   繁体   English

Matlab加速嵌套for循环

[英]Matlab speed up nested for loops

I have the following nested for loop program and it is taking forever to run. 我有以下嵌套的循环程序,它需要永远运行。 Is there a better way (more efficient) way to code this? 是否有更好的方法(更有效)编码方式?

tic 
ndx = 0;
for i1 = 1 : 49 - 5
    for i2 = i1 + 1 : 49 - 4
        for i3 = i2 + 1 : 49 - 3
            for i4 = i3 + 1 : 49 - 2
                for i5 = i4 + 1 : 49 - 1
                    for i6 = i5 + 1 : 49 - 0
                        ndx = ndx +1;
                         number(ndx,1) = i1;
                         number(ndx,2) = i2;
                         number(ndx,3) = i3;
                         number(ndx,4) = i4;
                         number(ndx,5) = i5;
                         number(ndx,6) = i6;
                       %  display([int2str(number(ndx,1)), ' ',  int2str(number(ndx,2)), ...
                        %     ' ',  int2str(number(ndx,3)), ' ',  int2str(number(ndx,4)),...
                        %    ' ',  int2str(number(ndx,5)), ' ',  int2str(number(ndx,6)) ])
                    end
                end
            end
        end
    end
end
toc

This answer is a digest of the above comments, as suggested: 这个答案是上述评论的摘要,如下所示:

To speed up nested for loops in MATLAB, the first things to consider include the following: Is an array/matrix growing inside a loop? 要在MATLAB中加速嵌套for循环,首先要考虑的事项包括:数组/矩阵是否在循环中增长? Can the whole calculation be vectorized, possibly using an existing MATLAB function? 可以使用现有的MATLAB函数对整个计算进行矢量化吗?

The answer to the first one is yes, number grows one line at a time in the innermost loop. 第一个答案是肯定的, number在最里面的循环中一次增长一行。 The answer to the second becomes irrelevant in this case once the first problem has been solved. 一旦第一个问题得到解决,第二个问题的答案在这种情况下变得无关紧要。

If you run your above code with only the assignment of ndx , not number , in the innermost loop, you will find out that the total number of lines you will get is 13983816, and that this is calculated very quickly. 如果在最里面的循环中只运行上面的代码而只分配ndx而不是number ,你会发现你得到的总行数是13983816,并且计算得非常快。 To prevent number from growing inside the loop, which requires rather costly memory operations, you could precede your code with: number = zeros(13983816,6); 为了防止number在循环内增长,这需要相当昂贵的内存操作,您可以在代码之前使用: number = zeros(13983816,6); This would create a double matrix of the size of the matrix you will get anyway, but with only 0 entries. 这将创建一个矩阵大小的double矩阵,但是只有0个条目。 Since you know all your entries will be from 1 to 49, the data type need not be double , uint8 suffices (for values from 0 to 255; HT:Amro). 由于您知道所有条目都是1到49,因此数据类型不必是doubleuint8就足够了(对于0到255之间的值; HT:Amro)。

So the first line of your code should be: 所以代码的第一行应该是:

number = zeros(13983816,6,'uint8');

Then the execution of the whole code will take only seconds. 然后执行整个代码只需几秒钟。

Note: As for the second question above, there is indeed an existing MATLAB function to do what the above code does, ie finding all combinations to choose 6 numbers out of 49 without repetition and without regards to order (German lottery?), but does it less efficiently and is thus only applicable to n much smaller than 49: nchoosek , which for two scalar inputs calculates the number of elements (ie nchoosek(49,6)==13983816 ) and for a vector (here: 1:49 ) and a scalar gives a matrix with the actual combinations. 注意:至于上面的第二个问题,确实存在一个MATLAB函数来执行上面的代码所做的事情,即找到所有组合从49个中选择6个数字而不重复而不考虑订单(德国彩票?),但是效率较低,因此仅适用于小于49的n: nchoosek ,对于两个标量输入计算元素数量(即nchoosek(49,6)==13983816 )和矢量(此处: 1:491:49 )并且标量给出具有实际组合的矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM