Removing NaN from matrix

조회 수: 4 (최근 30일)
Eric
Eric 2014년 7월 22일
편집: Patrik Ek 2014년 7월 29일
Hi,
I have a loop that stores data for each trial into a temp variable called temp(:,tnum).
The trials are of unequal row length. At the end of the trials (tnum), there is a NaN for n number of rows.
I would like to remove the NaN from each trial. Ultimately, regardless of the length of the trial I need to normalize each trial to 100 data points in order to take the ensemble average of the 5 trials.
for tnum = 1:5
temp(:,tnum) = DATA.(subjs{s}).(trial)(tnum).(jnt{j}).(var{v}).(dir{d}).data;
templin(:,tnum) = linspace(0,100,(size(temp(:,tnum),1)))';
xaxis = linspace(0,100,100)';
tempnorm(:,tnum) = spline(temlin(:,tnum),temp(:,tnum),xaxis);
end
This code run but I get a warning Warning: All data points with NaN in their value will be ignored. > In polyfun\private\chckxy at 101 In spline at 54
However, the last 20 or 30 of the 100 data points of
tempnorm(:,tnum) are not correct.
If anyone could me with my issue, I would greatly appreciate it.
Eric

답변 (2개)

Hikaru
Hikaru 2014년 7월 29일
If all columns contain n rows of NaN values, then you could remove them by using:
temp(isnan(temp(:,1)),:) = []

Patrik Ek
Patrik Ek 2014년 7월 29일
편집: Patrik Ek 2014년 7월 29일
You could choose to only pass non nan elements to spline.
nonNanTemlin = ~isnan(temlin(:,tnum));
tempnorm(:,tnum) = spline(temlin(nonNanTemlin,tnum),temp(nonNanTemlin,tnum),xaxis);
However, would it not be better with a weighted average here? From what I remember of statistics, that is the correct way of do this. Interpolating does not necessarily make the variance lower.

카테고리

Help CenterFile Exchange에서 Splines에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by