- 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 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Ayush
on 9 Oct 2023
Edited: Ayush
on 9 Oct 2023
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 Comments
Steven Lord
on 25 Oct 2023
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.)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!