Better work through indecies in a for loop

1 回表示 (過去 30 日間)
Poison Idea fan
Poison Idea fan 2022 年 9 月 13 日
コメント済み: Poison Idea fan 2022 年 9 月 13 日
I use a for loop to compute the absorption of aerosol particles in time series data. I indicate the start and stop of sample time with indecies stored in two arrays. When I compute the absorption I average four minutes of filter time and I do it based on going backwards in index. I run into a problem when the filter time isn't long enough to go back four minutes in time. My work around is using an if statement but it seems cumbersome to do it the way I have below. Is there a better way to do this?
The code below is just meant to be an example. I don't have a workspace small enough to upload to run the code.
for n = 1:length(start0)
if start0(n)-240 < 360 % If the filter time is not big enough then move on to the next sample time
n = n+1;
if start0(n)-240 < 360 % if the second filter time is too small move on to the third
n = n+1;
end
else
n = n;
end
absorption(start0(n):stop0(n),:) = ...
((smoothed_data_table.('Signal')(start0(n):stop0(n),:))...
-mean(smoothed_data_table.('Signal')(start0(n)-250:start0(n)-10,:),1,'omitnan'))...
./(smoothed_data_table.('power')(start0(n):stop0(n),:) .* mic_cal);
ext(start0(n):stop0(n),:) = (rL(1,:)/c).* ...
((smoothed_data_table.('Tau')(start0(n):stop0(n),:).^-1)...
-(mean(smoothed_data_table.('Tau')(start0(n)-250:start0(n)-10,:),'omitnan')).^-1);
end

採用された回答

David Hill
David Hill 2022 年 9 月 13 日
Just use continue.
for n = 1:length(start0)
if start0(n)-240 < 360 % If the filter time is not big enough then move on to the next sample time
continue;
end
absorption(start0(n):stop0(n),:) = ...
((smoothed_data_table.('Signal')(start0(n):stop0(n),:))...
-mean(smoothed_data_table.('Signal')(start0(n)-250:start0(n)-10,:),1,'omitnan'))...
./(smoothed_data_table.('power')(start0(n):stop0(n),:) .* mic_cal);
ext(start0(n):stop0(n),:) = (rL(1,:)/c).* ...
((smoothed_data_table.('Tau')(start0(n):stop0(n),:).^-1)...
-(mean(smoothed_data_table.('Tau')(start0(n)-250:start0(n)-10,:),'omitnan')).^-1);
end
  1 件のコメント
Poison Idea fan
Poison Idea fan 2022 年 9 月 13 日
I didn't realize that was an option. Thanks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by