フィルターのクリア

Is it possible to vectorize this loop?

1 回表示 (過去 30 日間)
Senaasa
Senaasa 2014 年 8 月 26 日
回答済み: Andrei Bobrov 2014 年 8 月 26 日
Hi,
I've been trying to figure out a way to optimize this bit of code by vectorizing the loop. I can't find a good way to do it--maybe it's not possible given the conditional used. In the actual program the array will contain 80,000+ elements so I'm trying to speed things up.
Thanks, Charles
%Test Array
A =[1 2 3 4 5 6;4 5 6 5 6 7;7 8 9 10 11 12;];
B=sum(A,1);
C=mean(B);
E=30*C/100;
F=and(B>=C-E,B<=C+E);
for i=1:2:length(F)-1
if or(F(i)==0,F(i+1)==0)
F(i)=0;
F(i+1)=0;
end
end
G=B(F);

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 26 日
A =[1 2 3 4 5 6;4 5 6 5 6 7;7 8 9 10 11 12;];
B=sum(A,1);
C=mean(B);
E=30*C/100;
F=and(B>=C-E,B<=C+E);
F=repmat(all(reshape(F,2,[])),2,1)
F=F(:)'

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 8 月 26 日
F = kron(all(reshape(F,2,[])),[1 1]);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by