MATLAB: Find function exceeds matrix dimensions

findmatrixmatrix dimensions

I'm trying to remove rows that contain Nans in two columns of a dataset. The dataset itself is 9895 x 26, but when I run the following code,
hh1 = hh1(~isnan(hh1(:, 19:20)),:);
it returns 'Index exceeds matrix dimensions' Further investigation revealed that when I run
find(~isnan(hh1(:, 19:20)))
it returns values ranging from 7037 to 19790, which is a problem because, as previously mentioned, the size of the dataset is only 9895 x 26.
Any idea what's going on?

Best Answer

Depending on what you want, if the two columns should be equal to nan
hh1 = hh1(~all(isnan(hh1(:,19:20)),2),:)
%or if at least one column is equal to nan
hh1 = hh1(~any(isnan(hh1(:,19:20)),2),:)