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