how to write this without loop

1 回表示 (過去 30 日間)
Rica
Rica 2013 年 3 月 1 日
hi! i wrote this in matlab:
%
r is a mtrix of the size(25*30000)
for l=1:30000
for k=2:25
d(k,l)=r(k,l)-r(k-1,l);
if d(k,l)<-109
r(k,l)=r(k,l)+300;
elseif d(k,l)>180
r(k,l)=r(k,l)-300;
end
end
end
how could write this with minimal loops?
thank you

採用された回答

Jan
Jan 2013 年 3 月 1 日
編集済み: Jan 2013 年 3 月 1 日
d = [zeros(1, 30000); diff(r)];
index = (d < -109);
r(index) = r(index) + 300;
index = (d > 180);
r(index) = r(index) - 300;
Or:
d = [zeros(1, 30000); diff(r)];
shift = zeros(size(r));
shift(d < -109) = 300;
shift(d > 180) = -300;
r = r + shift;
  2 件のコメント
Rica
Rica 2013 年 3 月 1 日
this is really professional :-). how to get this level?
thanks
Rica
Rica 2013 年 3 月 1 日
there is a proble with usin diff.in may loop i use the actual value to make the difference
%
d(k,l)=r(k,l)-r(k-1,l);
with the actual value of *d* means the corrected value with the if statement:
%
if d(k,l)<-109
r(k,l)=r(k,l)+300;
elseif d(k,l)>180
r(k,l)=r(k,l)-300;
thank you

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 3 月 1 日
You can start with diff(r), and then use logical indexing.

カテゴリ

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