What is a good way (other than ~=) to represent that a variable does not contain NaN values in R2013?

1 vue (au cours des 30 derniers jours)
My current code is aiming to perform basic arithmetic on a variable, but only if it contains values. I don't want to necessarily remove null values, but just find a good way of saying if my variable, data, actually contains some threshold of values, it can continue to do this basic arithmetic. My current code is as follows:
if data ~= isnan
average_length = 150;
means = [];
datalength = size(data,1);
speed =data(:,1);
insta_rate = data(:,2);
for count = 1:average_length:datalength
if count+average_length > datalength
mean_speed = mean(speed(count:datalength));
mean_insta_rate = mean(mean(count:datalength));
end
end
end
Thanks! :)

Réponse acceptée

Geoff Hayes
Geoff Hayes le 30 Juil 2014
Katherine - what do you mean by contains some threshold of values? If all values are NaN then obviously there is nothing to do, but do you wish to do "something" if at least one row in your data matrix does not include NaNs in both columns?
Note that your if condition won't work because there is no input argument to the isnan function, and comparing a matrix (data) with another in a if condition is not really what you want either.
I know that you said that you don't want to necessarily remove NaN values, but without them there you would be able to some work once the threshold has been applied
% define a threshold (so if at least 75% of rows are not-NaN, then use data)
threshold = 0.75;
% determine those rows of data that don't have a NaN in both columns
nonNanRows = ~isnan(data(:,1)) & ~isnan(data(:,2));
if sum(nonNanRows)/length(data) > threshold
% reset data to that which has the non-NaN data
data = data(nonNanRows,:);
% continue with your code to extract the speed, etc.
end
Try the above and see what happens!

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by