Better work through indecies in a for loop

조회 수: 2 (최근 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 CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by