MATLAB: How to separate vector into vectors of different lengths

vector separation mat2cell

I have a vector y=[1:1:10] and length vector l=[2,5,10] % these are the sample number of the vector y; I want to separate the y vector so that y1=[1,2]; y2=[3,4,5]; y3=[6,7,8,9,10];

Best Answer

y=[1:1:10] ;
l=[2,5,10] ;
y1=[1,2] ;
y2=[3,4,5] ;
y3=[6,7,8,9,10] ;
iwant = cell(length(l),1) ;
iwant{1} = y(1:l(1)) ;
for i = 2:length(l)
iwant{i} = y(l(i-1)+1:l(i)) ;
end