From: Tom Toner on
The methods above seem to have some errors in the results.

I wrote a code based on us' method:

clear all
len = 10000;
r = randi([0 1],1,len);
tic;
i0 = find(r==0);
k = i0(2:2:length(i0));
i1 = r==0;
i2 = double(i1);
i2(k) = 2;
[mstr,sstr]=regexp(char(i2+'0'),'1[0]+2','match','split');
mstr1 = regexprep(mstr,'0','3');
str0 = [sstr; [mstr1 {''}]];
str = [str0{:}];
ind = strfind(str,'3');
toc

The result shows:
Elapsed time is 0.046551 seconds.

For the vector is only 30 in length, it takes around 0.02 sec.
So it seems pretty expensive in string manipulations.

I was wondering if there were good ways to do it.

thanks,