Compute mean for multiple different length data
1 view (last 30 days)
Show older comments
Hello, I’ve been searching a solution for a while but to little avail. I have many data files with different sizes saved as .mat . I need to load them in and compute their means at the same X value. The file looks alike:
file1: -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 -- X value 0.17 0.25 0.14 0.54 0.5 0.34 0.11 0.33 0.91 1.0 0.72 0.65 0.83 0.32 -- data
file2: -1 0 1 2 3 4 -- X value 0.85 0.37 0.41 0.58 1.0 0.73 -- Data
file3, file4, etc…
I have a ‘ for’ loop to load file and get X and data, how to program them to have the same length and compute mean? I would like to patch the missing data point with NaN. The X value is increment of 1. I need your kind suggestions. Thank you!
Accepted Answer
Jan
on 2 Aug 2011
You can use 2 loops: One for reading and the 2nd for the computations:
XList = zeros(2, nFile);
DataList = cell(1, nFile);
for iFile = 1:nFile
% Get X and Data from file:
...
XList(1:2, iFile) = [X(1); X(end)];
DataList{iFile} = Data;
end
Xmin = min(XList(1, :));
Xmax = max(XList(2, :));
Xnum = Xmax - Xmin + 1;
DataSum = zeros(1, Xnum);
DataNum = zeros(1, Xnum);
for iFile = 1:nFile
ini = XList(1, iFile) - Xmin + 1;
fin = XList(2, iFile) - Xmin + 1;
DataSum(ini:fin) = DataSum(ini:fin) + DataList{iFile};
DataNum(ini:fin) = DataNum(ini:fin) + 1;
end
DataMean = DataSum ./ DataNum;
More Answers (1)
Fangjun Jiang
on 2 Aug 2011
To fill the missing data with nan, use the following code. But once you fill it with nan, you won't be able to calculate a meaningful mean value, so you need to decide before proceeding to the next step.
x=[-1 0 1 2 3 4];
data=[0.85 0.37 0.41 0.58 1.0 0.73];
All_X=-10:10;
NewData=nan(size(All_X));
Index=ismember(All_X,x);
NewData(Index)=data;
0 Comments
See Also
Categories
Find more on Biological and Health Sciences 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!