0

我有大量的 for 循环;有什么办法可以让这个运行得更快吗?获取散点图需要 30 多分钟。

我在想也许可以使用休息。是否有可能或者我需要使用其他东西来让程序逐段运行?

A = [4/5 -1/5 -1/5 -1/5 -1/5; -1/5 4/5 -1/5 -1/5 -1/5; -1/5 -1/5 4/5 -1/5 -1/5;-1/5 -1/5 -1/5 4/5 -1/5; -1/5 -1/5 -1/5 -1/5 4/5];
B = [ 0 0 0 0 0 ; 0 0 0 0 1 ; 0 0 0 1 0 ; 0 0 0 1 1 ; 0 0 1 0 0 ; 0 0 1 0 1 ];

for i = 1:size(B,1)
    p1 = A * B(1,:)' -A * B(i,:)';
    dtransformation0a(i) = d*p1;
    qtransformation0a(i) = q*p1;

    for i = 1:size(B,1)
        p2 = A * B(2,:)' -A * B(i,:)';
        dtransformation0b(i) = d*p2;
        qtransformation0b(i) = q*p2;

        for i = 1:size(B,1)
            p3 = A * B(3,:)' -A * B(i,:)';
            dtransformation0c(i) = d*p3;
            qtransformation0c(i) = q*p3;

            for i = 1:size(B,1)
                p4 = A * B(4,:)' -A * B(i,:)';
                dtransformation0d(i) = d*p4;
                qtransformation0d(i) = q*p4;

                for i = 1:size(B,1)
                    p5 = A * B(5,:)' -A * B(i,:)';
                    dtransformation0e(i) = d*p5;
                    qtransformation0e(i) = q*p5;

                    for i = 1:size(B,1)
                        p6 = A * B(6,:)' -A * B(i,:)';
                        dtransformation0f(i) = d*p6;
                        qtransformation0f(i) = q*p6;
                    end
                end
            end
        end
    end
end

figure
scatter(dtransformation0a,qtransformation0a,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right') 
hold on 
scatter(dtransformation0b,qtransformation0b,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right')
hold on 
scatter(dtransformation0c,qtransformation0c,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right')
hold on 
scatter(dtransformation0d,qtransformation0d,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right')
hold on
scatter(dtransformation0e,qtransformation0e,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right') 
hold on 
4

2 回答 2

2

我不相信您需要嵌套所有这些循环,因为您似乎正在重新定义i每个单独循环的每个单独迭代中的值。单个循环也应该可以工作。这应该同样有效,但要快得多。

A = [4/5 -1/5 -1/5 -1/5 -1/5; -1/5 4/5 -1/5 -1/5 -1/5; -1/5 -1/5 4/5 -1/5 -1/5;-1/5 -1/5 -1/5 4/5 -1/5; -1/5 -1/5 -1/5 -1/5 4/5];
B = [ 0 0 0 0 0 ; 0 0 0 0 1 ; 0 0 0 1 0 ; 0 0 0 1 1 ; 0 0 1 0 0 ; 0 0 1 0 1 ];

for i = 1:size(B,1)
    p1 = A * B(1,:)' -A * B(i,:)';
    dtransformation0a(i) = d*p1;
    qtransformation0a(i) = q*p1;
    p2 = A * B(2,:)' -A * B(i,:)';
    dtransformation0b(i) = d*p2;
    qtransformation0b(i) = q*p2;
    p3 = A * B(3,:)' -A * B(i,:)';
    dtransformation0c(i) = d*p3;
    qtransformation0c(i) = q*p3;
    p4 = A * B(4,:)' -A * B(i,:)';
    dtransformation0d(i) = d*p4;
    qtransformation0d(i) = q*p4;
    p5 = A * B(5,:)' -A * B(i,:)';
    dtransformation0e(i) = d*p5;
    qtransformation0e(i) = q*p5;
    p6 = A * B(6,:)' -A * B(i,:)';
    dtransformation0f(i) = d*p6;
    qtransformation0f(i) = q*p6;
end

figure
scatter(dtransformation0a,qtransformation0a,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right') 
hold on 
scatter(dtransformation0b,qtransformation0b,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right')
hold on 
scatter(dtransformation0c,qtransformation0c,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right')
hold on 
scatter(dtransformation0d,qtransformation0d,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right')
hold on
scatter(dtransformation0e,qtransformation0e,100,'b.')

%text(dtransformation0a(:), qtransformation0a(:), labels, 'VerticalAlignment','bottom', ...
% 'HorizontalAlignment','right') 
hold on 

换句话说,时间复杂度已经从 降低O(n^6)O(n)。因此,如果您之前花费 30 分钟,您可能会花费大约 30^(1/6) 分钟,即现在不到 2 分钟。

于 2013-03-07T05:22:57.703 回答
1

我同意罗尼的观点,

不需要嵌套所有循环。

这些将有助于将时间减少到 n*6-1(大约)

于 2013-03-07T05:39:05.297 回答