Unusual result when removing NaNs
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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
per isakson
2015년 2월 4일
Sound very strange!
James Tursa
2015년 2월 4일
편집: James Tursa
2015년 2월 4일
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
2015년 2월 5일
Daniel Robbins
2015년 2월 5일
Daniel Robbins
2015년 2월 5일
답변 (1개)
per isakson
2015년 2월 4일
편집: per isakson
2015년 2월 4일
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
이 질문은 마감되었습니다.
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!