Better work through indecies in a for loop

1 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by