Averaging non-aligned time-series arrays
8 ビュー (過去 30 日間)
古いコメントを表示
I have four arrays of time series data, sampled at equal intervals. The arrays do not start or end at the same times. So I have eight arrays (time values are integers):
- time1, data1
- time2, data2
- time3, data3
- time4, data4
How can I create a mean time series from all four, covering the entire time range? I would assume NaN where there is no data.
For example, with two small time series:
time1 = [1 2 3 4];
data1 = [1 1 1 1];
time2 = [3 4 5];
data2 = [3 3 3];
I want the result:
timeMean = [1 2 3 4 5];
dataMean = [1 1 2 2 3]
I could do this with several loops, but is there an elegant way?
0 件のコメント
回答 (2 件)
Star Strider
2024 年 9 月 4 日
Try something like this —
time1 = [1 2 3 4];
data1 = [1 1 1 1];
time2 = [3 4 5];
data2 = [3 3 3];
TT1 = timetable(seconds(time1.'), data1.')
TT2 = timetable(seconds(time2.'), data2.')
TTs = synchronize(TT1,TT2)
TTs.dataMean = mean(TTs{:,[1 2]}, 2, 'omitmissing');
TTs = removevars(TTs,[1 2])
.
0 件のコメント
Zinea
2024 年 9 月 5 日 8:22
For creating a mean time series covering the entire time range, you can use a combination of ‘unique’, ‘ismember’, and vectorized operations to avoid explicit loops.
Given below are the functions used followed by the code assuming dummy values for the arrays:
- All time arrays are concatenated, and ‘unique’ is used to find all distinct time points.
- A ‘dataMean’ array is created and filled with ‘NaN’ values initially.
- For each unique point, the code checks which series contain data for that time, collects those values, and computes their mean ignoring ‘Nan” values.
% Define the time and data arrays
time1 = [1 2 3 4];
data1 = [1 1 1 1];
time2 = [3 4 5];
data2 = [3 3 3];
time3 = [2 3 4 5 6];
data3 = [2 2 2 2 2];
time4 = [1 2 6 7];
data4 = [4 4 4 4];
% Combine all time arrays and find the unique time points
allTimes = [time1, time2, time3, time4];
timeMean = unique(allTimes);
% Initialize the dataMean array with NaNs
dataMean = NaN(size(timeMean));
% Function to compute mean ignoring NaNs
nanmeanFunc = @(x) mean(x(~isnan(x)));
% Loop over each unique time point to compute the mean
for i = 1:length(timeMean)
currentTime = timeMean(i);
% Find data values corresponding to the current time in each series
dataValues = NaN(1, 4); % Assuming there are 4 data series
[~, idx1] = ismember(currentTime, time1);
if idx1 > 0
dataValues(1) = data1(idx1);
end
[~, idx2] = ismember(currentTime, time2);
if idx2 > 0
dataValues(2) = data2(idx2);
end
[~, idx3] = ismember(currentTime, time3);
if idx3 > 0
dataValues(3) = data3(idx3);
end
[~, idx4] = ismember(currentTime, time4);
if idx4 > 0
dataValues(4) = data4(idx4);
end
% Compute the mean of the available data values
dataMean(i) = nanmeanFunc(dataValues);
end
% Display the result
disp('timeMean = ');
disp(timeMean);
disp('dataMean = ');
disp(dataMean);
Output:
Hope this resolves the query!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!