Removing NaN from matrix

4 views (last 30 days)
Eric
Eric on 22 Jul 2014
Edited: Patrik Ek on 29 Jul 2014
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

Answers (2)

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

Patrik Ek
Patrik Ek on 29 Jul 2014
Edited: Patrik Ek on 29 Jul 2014
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.

Community Treasure Hunt

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

Start Hunting!