フィルターのクリア

Compute mean for multiple different length data

1 回表示 (過去 30 日間)
Yonghe
Yonghe 2011 年 8 月 2 日
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 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 8 月 2 日
Vertical means?
Yonghe
Yonghe 2011 年 8 月 2 日
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, ...

サインインしてコメントする。

採用された回答

Jan
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 件のコメント
Yonghe
Yonghe 2011 年 8 月 2 日
Jan & Fangjun, Thank you for your help! -- Yonghe

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
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;

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by