Compute mean for multiple different length data
1 次查看(过去 30 天)
显示 更早的评论
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!
采纳的回答
Jan
2011-8-2
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;
更多回答(1 个)
Fangjun Jiang
2011-8-2
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Biological and Health Sciences 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!