Info

This question is closed. Reopen it to edit or answer.

Unusual result when removing NaNs

1 view (last 30 days)
Daniel Robbins
Daniel Robbins on 4 Feb 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi,
I have a large vector, 59787 data points, of which only 427 are numbers, the remaining are NaN's. In the workspace this is displayed as double.
I tried to remove the Nan's using the following command:
test2(~isnan(test2));
Yet the result was the vector reduced to a vector 51673 of data points, none of which were NaN's. Essentially a whole load of new randon numbers had been added to the original 427 data points.
After playing around I noticed that if I reduce the vector via the following command:
test2=test(1:8500);
Then used the same command to remove the NaN's:
test2(~isnan(test2));
The result was a vector 427 number long, i.e. the result I was looking for. Though if I go to 8600 I get vector 486 long (including random numbers). The amount of random numbers added increases again if I change to 8600 or more.
Can anyone shed any light on why this would be happening?
  5 Comments
Daniel Robbins
Daniel Robbins on 5 Feb 2015
Update, I just chceked the stucture created by 'importdata', the datapoints are all NaN's there, so it seems to be something to do with the command
test=a.data(1:end);
Where I create a variable for just one vector.
Any ideas why this would happen?
Daniel Robbins
Daniel Robbins on 5 Feb 2015
Sussed it, I should be using
test=a.data(:,1);
Thanks to everyone for your help

Answers (1)

per isakson
per isakson on 4 Feb 2015
Edited: per isakson on 4 Feb 2015
Replace
test2(~isnan(test2));
by
test2(isnan(test2)) = [];
which is the "standard" way to remove nans.
&nbsp
Run this script
m1 = rand(1,1e6);
nnn = 1e3;
isn = false(size(m1));
isn(randi(1e6,[1,nnn]))= true;
m1(isn) = nan;
m2 = m1;
m2(isnan(m2)) = [];
length(m1) - length(m2)
it returns 1000 as expexted

Products

Community Treasure Hunt

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

Start Hunting!