Q:

Matlab: find small islands of numbers surrounded by NaN

I have a lengthy vector of numeric data, with some sequences of NaNs here and there. Most of the NaNs come in large chunks, but sometimes the segments of NaNs are close together, creating islands of numbers surrounded by NaNs like this:
...NaN 1 2 3 5  ... 9 4 2 NaN...

I would like to find all islands of data that are between 1 - 15000 elements in size and replace them by a solid block of NaNs. 
I've tried a few things, but there are some problems --the data set is HUGE, hence converting it to a string and using a regular expression to do: 
[found start end] = regexp(num2str(isnan(data)),'10{1,7}1','match','start','end')

is out of the question because it takes prohibitively long to do  num2str(isnan(data)). So I  need a numeric way to find all NaN-numbers-Nan where the number of numbers is between 1 and 15000.

A:

Here is an example how you can do that:
% generate random data
data            = rand(1,20)
data(data>0.5)  = NaN

% add NaN before and after the original array
% for easier detection of the numerical block
% starting at 1st element and finishing at the last one
datatotest      = [ NaN data NaN ];

NumBlockStart   = find( ~isnan(datatotest(2:end))   & isnan(datatotest(1:end-1)) )+0
NumBlockEnd     = find( ~isnan(datatotest(1:end-1)) & isnan(datatotest(2:end))   )-1
NumBlockLength  = NumBlockEnd - NumBlockStart + 1

In this example NumBlockStart contains start index of numeric block and NumBlockEnd contains last index of numeric block. NumBlockLength contains length of each block.
Now you can do whatever you want to them :)
Here is possible output
data =
0.0382    0.3767    0.8597    0.2743    0.6276    0.2974    0.2587    0.8577    0.8319    0.1408    0.9288    0.0990    0.7653    0.7806    0.8576    0.8032    0.8340    0.1600    0.4937    0.7784

data =
0.0382    0.3767       NaN    0.2743       NaN    0.2974    0.2587       NaN       NaN    0.1408       NaN    0.0990       NaN       NaN       NaN       NaN       NaN    0.1600    0.4937       NaN

NumBlockStart =
 1     4     6    10    12    18

NumBlockEnd =
 2     4     7    10    12    19

NumBlockLength =
 2     1     2     1     1     2

UPDATE1
This is more efficient version:
data            = rand(1,19)
data(data>0.5)  = NaN

test2           = diff( ~isnan([ NaN data NaN ]) );
NumBlockStart   = find( test2>0 )-0
NumBlockEnd     = find( test2<0 )-1
NumBlockLength  = NumBlockEnd - NumBlockStart + 1

