Matlab - missing data Flashcards
Real life psychology experiments often suffer from missing data - you may want to exclude trials
where reaction times fall outside some limits, or where the subject made an error. In Matlab, you can
do this using NaN. NaN stands for Not A Number and can be used in any vector or matrix in place of
missing data. In general, it is much better to replace your missing data with NaNs than to delete it
altogether. When you use NaN, some of the basic functions like sum and mean will no longer work as
10
you expect, but Matlab provides alternate versions, nansum, nanmean and nanstd which let you
ignore NaNs and find the sum, mean and standard deviation of your data
if
» err = [1 0 0 0 0 0 0 1 0 1 0 0];
how would you replace an error (1s) using NaN?
data(err==1) = NaN
using the cat data:
» data = [4 14 6 11 3 14 8 17 17 12 10 18];
» cat = [1 3 2 1 2 2 3 1 3 2 3 1];
» cat2 = cat==2
cat2 = 0 0 1 0 1 1 0 0 0 1 0 0
» data2 = data(cat2);
data2 = 6 3 14 12
an additional vector err has a one at every error:
err = [1 0 0 0 0 0 0 1 0 1 0 0];
how would you code so that every error is replaced with a NaN?
data(err==1) = NaN
how would you find the mean following this line of code?
» data2 = data(cat2);
data2 = 6 3 14 NaN
> > mdat2 = nanmean(data2)