Unusual result when removing NaNs
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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
per isakson
on 4 Feb 2015
Sound very strange!
James Tursa
on 4 Feb 2015
Edited: James Tursa
on 4 Feb 2015
What MATLAB version and machine are you using? Your first effort looks fine to me, and works OK on R2014a Win64:
>> test2 = nan(59787,1);
>> test2(1:140:end) = 1;
>> size(test2(~isnan(test2)))
ans =
428 1
Are you sure of the number of NaN's in your original test2 vector? What do you get with sum(isnan(test2))?
Daniel Robbins
on 5 Feb 2015
Daniel Robbins
on 5 Feb 2015
Daniel Robbins
on 5 Feb 2015
Answers (1)
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.
 
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
This question is closed.
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!