Info

この質問は閉じられています。 編集または回答するには再度開いてください。

For Loop With Non-Intergers and Looping Over Multiple Values in One Step

1 回表示 (過去 30 日間)
Lewis Holden
Lewis Holden 2017 年 2 月 9 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I am trying to loop through data which contains measurements every hourly, half hour and 20 minutes by making my timestep one_hour (0.0417 in MATLAB time). There are two issues because:
1) my timestep isn't an interger 2) if the data is half hourly/every 20 minutes I need to compute a mean for each hour, rather than simply appending each value even if it is from each half hour/20 minutes.
This is completely beyond my expertise so would appreciate any and all help.
first_january_1987 = datenum([1987 01 00 00 00 00]);
first_feburary_1987 = datenum([1987 02 00 00 00 00]);
% loop over
% define strength of gale
gale_force_wind = 17.5
% no of hours in a month to add gale hours to
hours_of_gale_january = zeros([1, round(one_month/one_hour)])
% find the indices of where jan 1987 begins and ends
x = find(aldergrove_wind_data_QC == first_january_1987)
y = find(aldergrove_wind_data_QC == first_feburary_1987)
for k = aldergrove_wind_data_QC(:,[x:one_hour:y])
if aldergrove_wind_data_QC(k) > gale_force_wind;
for j = 1:round(one_month/one_hour)
hours_of_gale_january(j) = aldergrove_wind_data_QC(:,3);
end
end
end
  1 件のコメント
John D'Errico
John D'Errico 2017 年 2 月 9 日
The word is INTEGER. Only one r.

回答 (1 件)

D. Plotnick
D. Plotnick 2017 年 2 月 10 日
IF I understand your question (try providing a minimal working example next time of what you want to do, below is a sample MWE), the best way to proceed is using interp1 to place your data onto the grid that you want.
The MWE I have below makes up some data, and then does the hourly lookup and averaging that you want. It will be up to you to get it working on your data.
data = 2*rand(1000,1)-1; % just some random data
% Lets smooth that data so it doesnt look as gross.
data = medfilt2(data,[10,1]);
% Lets make a random set of timesteps every 20, 30, and 60 minutes.
data_collect_time = ceil(3*rand(1000,1));
data_collect_time(data_collect_time == 1) = 20;
data_collect_time(data_collect_time == 2) = 30;
data_collect_time(data_collect_time == 2) = 60;
% do a cumulative sum to get our time vector;
time = cumsum(data_collect_time)/60;
We now have a time vector with uneven differences in time. but, importantly, it is monotonic and increasing. Your data might be better organized than this, and it might be in hours, minutes, seconds, etc. Ours is in hours
ok lets show the vector we want to actually measure on
start_time = 45; % start, here it is 45 hours, for you it will be Jan 1 converted into
% the same space as your 'time'; I guess its your aldergrove_wind_data_QC.
Really, its important to get everything on the same space, and not in MDY format.
end_time = 220; % lets stop here, your Feb1.
time_new = start_time:1:end_time; % step size of one hour, but we could do any spacing we wanted
now the magic, we just interpolate onto a new grid. This does a linear interpolation, so it will 'average' over points if they are closer together than one hour.
data_new = interp1(time,data,time_new,'linear');
Lets plot our data so you can see.
plot(time,data); hold on; plot(time_new,data_new);
So, you can see that (a) your start/stop points are correct, (b) the data appropriately tracks the original data, and averages over nearby points, (c) is now measured every hour exactly.
This is far from all you can do with interp. Go, explore the possibilities!
  1 件のコメント
Lewis Holden
Lewis Holden 2017 年 2 月 11 日
Thanks - this is really helpful, exactly the function I need. Unfortunately, because of the non-unique nature of the wind speed data(example below) the function doesn't work as all points need to be unique. Any idea how to get round that?
Thanks in Advance.
lewis
[22 22 22 21 20 20 18 17]

Community Treasure Hunt

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

Start Hunting!

Translated by