MATLAB: Sequence grouping

array

Hi all,
I am trying to construct a few groups from a given sequence by giving some pivots, one or more. For example, a = 1:15 and the pivots are 6 and 9. The desired groups are [1:3], [4:8], [7:11] and [12:15]. Is loop (brute force) the only solution?
for i = 1:length(piv)
par{i} = piv(i)-length(piv):piv(i)+length(piv);
end
par1 = 1:piv(1)-1;
par2 = piv(2)+length(piv)+1:end;
Thanks.

Best Answer

Does this do what you are wanting?
Lp = length(piv);
G = ones(Lp,2*Lp+1);
G(:,1) = piv-Lp;
G = cumsum(G,2);
D{1} = min(a):min(G(:))-1; % Holds the groups
D(2:Lp+1) = mat2cell(G,ones(1,Lp),2*Lp+1);
D{Lp+2} = max(G(:))+1:max(a);
.
EDIT
.
Note that this may be much faster, though it does have a loop.
L2 = length(piv);
D2 = cell(L2+2,1);
D2{1} = min(a):min(piv-L2-1);
for ii = 2:L2+1
D2{ii} = piv(ii-1)-L2:piv(ii-1)+L2;
end
D2{L2+2} = max(piv+L2)+1:max(a);