function [IDnans,nrNANs]=findmultipleNANs(X,numberofNANs)

X=X(:);

%% determine the number of consecutive NAN's
% ------- replaced by a faster alternative ----------
% if two nan's after each other the ID of both NAn's will be 2
% tic
% Xnan = isnan(X);
% nrNANs=[];
% cntr=0;
% for ii=1:length(Xnan)
%     if Xnan(ii)==1
%         cntr=cntr+1;
%         for ii1=ii:-1:ii-(cntr-1)
%             nrNANs(ii1)=cntr;
%         end
%     else
%         nrNANs(ii)=0;
%         cntr=0;
%     end
% end
% toc

X1 = [0;X(:);0];
nrNANs = zeros(size(X));
IDXnotnan   = find(~isnan(X1));
NANlength   = diff(IDXnotnan)-1;
IDNANlength = find(NANlength~=0);
for ii=1:length(IDNANlength)
    value = NANlength(IDNANlength(ii));
    IDrange = [IDXnotnan(IDNANlength(ii)):IDXnotnan(IDNANlength(ii)+1)-2]';
    nrNANs(IDrange)=value;
end

IDnans = find(nrNANs>numberofNANs);




