- Find maximum number of data points in a run by using: “max(cellfun(@(x) size(x, 1), data))”
- Create an empty matrix to store the interpolated values: “zeros(maxPoints, 3);”
- Determine number of data points using: ”size(data{i}, 1);”
- Linearly interpolate missing data using: “interp1(1:numPoints, data{i}, linspace(1, numPoints, maxPoints));”
- Store the data and calculate the average
Averaging varying dimension (x y z) data over n cycles
1 次查看(过去 30 天)
显示 更早的评论
Hi there,
So I have some collected data. The data is split in runs and each run has points defined by x y z coordinates. I have n number of runs and I want to average the data over the n cycles. The problem is that I am missing some of the data points/the dimension of the runs is not consistent (eg.Run1 has 150 data points, Run2 has 154 data points). I don't know where in the run the data is missing from. Is there any straightforward way of doing this?
0 个评论
回答(1 个)
Ayush
2023-10-9
编辑:Ayush
2023-10-9
Hi Daniela,
I understand that you are trying to average the data given in (x,y,z) coordinates over ‘n’ runs, where each run is having different data points due to missing data.
To solve this, you can use the interpolation method which generates missing data points. MATLAB provides several methods to interpolate, linear interpolation being one of them. After interpolating you can take the average over the ‘n’ runs. Follow the below steps to interpolate the data and calculate the average:
An example code for the above steps is given below:
% Example data
n = 3; % Number of runs
data = cell(n, 1);
data{1} = [1 2 3; 4 5 6; 7 8 9]; % Run 1
data{2} = [10 11 12; 13 14 15]; % Run 2
data{3} = [16 17 18; 19 20 21; 22 23 24; 25 26 27]; % Run 3
% Step 1: Identify the maximum number of data points
maxPoints = max(cellfun(@(x) size(x, 1), data));
% Step 2: Loop through each run
for i = 1:n
% Step 2: Generate empty matrix
averagedData = zeros(maxPoints, 3);
% Step 3b: Determine the number of data points
numPoints = size(data{i}, 1);
% Step 3c: Interpolate missing data points
interpolatedData = interp1(1:numPoints, data{i}, linspace(1, numPoints, maxPoints));
% Step 3c: Store interpolated data
averagedData(1:maxPoints, :) = averagedData(1:maxPoints, :) + interpolatedData;
data{i} = averagedData;
disp(data{i})
end
% Step 4: Calculate the average
dim = ndims(data{1});
M = cat(dim+1,data{:});
meanArray = mean(M,dim+1);
disp(meanArray)
For more information on the “interp1” function, please refer to the following link:
Hope this helps!
Regards,
Ayush.
4 个评论
Steven Lord
2023-10-25
Please show us a small sample of your data with which we could experiment, and show us what you've already tried (adapting the solution in the answer at the top of this discussion.)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!