开发者

Matlab speed up nested for loops

开发者 https://www.devze.com 2023-03-14 02:03 出处:网络
I have the following nested for loop 开发者_JAVA技巧program and it is taking forever to run. Is there a better way (more efficient) way to code this?

I have the following nested for loop 开发者_JAVA技巧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? Can the whole calculation be vectorized, possibly using an existing MATLAB function?

The answer to the first one is yes, number grows one line at a time in the innermost loop. 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. To prevent number from growing inside the loop, which requires rather costly memory operations, you could precede your code with: 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. 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).

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, i.e. 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 (i.e. nchoosek(49,6)==13983816) and for a vector (here: 1:49) and a scalar gives a matrix with the actual combinations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号