フィルターのクリア

Averaging non-aligned time-series arrays

8 ビュー (過去 30 日間)
dormant
dormant 2024 年 9 月 4 日
回答済み: Zinea 2024 年 9 月 5 日 8:22
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?

回答 (2 件)

Star Strider
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.')
TT1 = 4x1 timetable
Time Var1 _____ ____ 1 sec 1 2 sec 1 3 sec 1 4 sec 1
TT2 = timetable(seconds(time2.'), data2.')
TT2 = 3x1 timetable
Time Var1 _____ ____ 3 sec 3 4 sec 3 5 sec 3
TTs = synchronize(TT1,TT2)
TTs = 5x2 timetable
Time Var1_TT1 Var1_TT2 _____ ________ ________ 1 sec 1 NaN 2 sec 1 NaN 3 sec 1 3 4 sec 1 3 5 sec NaN 3
TTs.dataMean = mean(TTs{:,[1 2]}, 2, 'omitmissing');
TTs = removevars(TTs,[1 2])
TTs = 5x1 timetable
Time dataMean _____ ________ 1 sec 1 2 sec 1 3 sec 2 4 sec 2 5 sec 3
.

Zinea
Zinea 2024 年 9 月 5 日 8:22
Hi dormant ,
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:
  1. All time arrays are concatenated, and ‘unique’ is used to find all distinct time points.
  2. A ‘dataMean’ array is created and filled with ‘NaN’ values initially.
  3. 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!

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by