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 comentarios

per isakson
per isakson el 4 de Feb. de 2015
Sound very strange!
James Tursa
James Tursa el 4 de Feb. de 2015
Editada: James Tursa el 4 de Feb. de 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
Daniel Robbins el 5 de Feb. de 2015
This was very helpfull, thank you. It appears the issue is not actually with the removal of NaN's but that the upload seems to be inventing some numbers from somewhere. The NaN's are actually the first 8114 datapoints (for want of a better word) after which the random numbers appear.
Perhaps I need to find a different way of loading the data from a textfile? The whole issue stems from trying to upload some signals which are different in length but saved in to the same .txt files.
I have tried 'dlmread' and 'textscan', but these do not produce any NaN's and just invent random numbers to fill the void in the shorter signal.
Daniel Robbins
Daniel Robbins el 5 de Feb. de 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 el 5 de Feb. de 2015
Sussed it, I should be using
test=a.data(:,1);
Thanks to everyone for your help

Respuestas (1)

per isakson
per isakson el 4 de Feb. de 2015
Editada: per isakson el 4 de Feb. de 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

La pregunta está cerrada.

Productos

Preguntada:

el 4 de Feb. de 2015

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by