필터 지우기
필터 지우기

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 CenterFile Exchange에서 Statistics 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