Info
This question is closed. Reopen it to edit or answer.
Unusual result when removing NaNs
1 view (last 30 days)
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
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
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!