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 に答える