Effacer les filtres
Effacer les filtres

Compute mean for multiple different length data

1 vue (au cours des 30 derniers jours)
Yonghe
Yonghe le 2 Août 2011
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!
  2 commentaires
Oleg Komarov
Oleg Komarov le 2 Août 2011
Vertical means?
Yonghe
Yonghe le 2 Août 2011
Hi Oleg,
I need the Data mean value at each of X vale for all the files. Such as, in my example, the mean Data value at X = -5, -4, -3, -2, -1, 0, 1, ...

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 2 Août 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;
  1 commentaire
Yonghe
Yonghe le 2 Août 2011
Jan & Fangjun, Thank you for your help! -- Yonghe

Connectez-vous pour commenter.

Plus de réponses (1)

Fangjun Jiang
Fangjun Jiang le 2 Août 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;

Catégories

En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by