フィルターのクリア

for loop

2 ビュー (過去 30 日間)
samia
samia 2011 年 4 月 27 日
how to use 'for' in a nested loop and get a result quickly for example I write this code:
%%%%%%%%%%%%%%%%%%
Ac=reshape(Ac,1,[]);
Ac_estm=reshape(Ac_estm,1,[]);
P=size(Ac,2);
T=reshape(T,1,[]);
for(p=1:P)
delta=d*Ac(p);
if(T(p)==0)
while(Ac(p)<=Ac_estm(p)-delta)
Ac(p)=Ac(p)-1;
end
else
while(Ac(p)>=Ac_estm(p)+delta)
Ac(p)=Ac(p)+1;
end
end
end
%%%%%%%%%%%%%
but the execution is very slow and stops over the counter at 9
(T is logical and length(T)>= length(Ac) and length(Ac)=length(Ac_estm))
  4 件のコメント
Andrei Bobrov
Andrei Bobrov 2011 年 4 月 27 日
while(Ac(p)<=Ac_estm(p)-delta)
Ac(p)=Ac(p)-1;
end
here perhaps Ac(p)-> -inf
while(Ac(p)>=Ac_estm(p)-delta)
Ac(p)=Ac(p)+1;
end
and here perhaps Ac(p)-> +inf
Andrei Bobrov
Andrei Bobrov 2011 年 4 月 27 日
use variant
while(Ac(p)<=Ac_estm(p)-delta)
Ac(p)=Ac(p)+1;
end
while(Ac(p)>=Ac_estm(p)-delta)
Ac(p)=Ac(p)-1;
end

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 4 月 27 日
variant without loop
delta = d*Ac;
t = T==0;
R = Ac - Ac_estm;
R1 = R + delta;
R2 = R - delta;
t1 = sign(R1) <= 0;
t2 = sign(R2) >= 0;
Ac(t & t1) = Ac(t & t1) + ceil(abs(R1(t & t1)));%changed sign
Ac(~t & t2) = Ac(~t & t2) - ceil(abs(R2(~t & t2)));%changed sign
variant with loop
Ac=reshape(Ac,1,[]);
Ac_estm=reshape(Ac_estm,1,[]);
P=size(Ac,2);
T=reshape(T,1,[]);
for p=1:P
delta=d*Ac(p);
if T(p)==0
t = Ac(p) - Ac_estm(p) + delta;
t1 = t(t<=0);
else
t = Ac(p) - Ac_estm(p) - delta;
t1 = t(t>=0);
end
Ac(p) = Ac(p) - sign(t1)*ceil(abs(t1));
end
  1 件のコメント
samia
samia 2011 年 4 月 28 日
thank you for your aid!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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