MATLAB: Sort values of a vector in an array according to gaps

logical?loopsave timesort

Hi. I have a vector that looks something like this:
A = [1; 2; 3; 4; 6; 7; 8; 9; 10; 12; 13; 14; 15; 16; 17; 19; 20];%and so on
and another vector containing the gaps in A:
B = [5; 11; 18]
In this example, the values 5, 11 and 18 are missing. The vector I am working with is a lot longer and has the values missing in random places.
What I want to do is create an array out of A that starts a new column every time there's a gap:
CELL = {[1; 2; 3; 4], [6; 7; 8; 9; 10], [12; 13; 14; 15; 16; 17], [19; 20]};
I wrote a loop that does just that, but because A is so long, my computer eventually freezes. Any suggestions for a faster method using logical operations or something (I'm not so good with those, yet).
Thanks in advance!

Best Answer

A = [1; 2; 3; 4; 6; 7; 8; 9; 10; 12; 13; 14; 15; 16; 17; 19; 20]
idx=diff(A);
ii2=unique([find(idx>1) ;numel(A)]);
ii1=[1 ;ii2(1:end-1)+1];
out=arrayfun(@(x,y) A(x:y),ii1,ii2,'un',0);
celldisp(out)