MATLAB: Index info for CAT

catfind

I am concatenating vectors x1,x2,x3 using y=cat(2,x1,x2,x3). Each one of these vectors have different lengths. I need to keep track index information for each one of them. How can I do this without loops.
x1=[1 1 0 1]; x2=[1 0]; x3=[1 1 1];
y=cat(2,x1,x2,x3);
= [1 1 0 1 1 0 1 1 1]
index = [1 4;5 6;7 9]

Best Answer

index=reshape(cumsum([1 numel(x1)-1 1 numel(x2)-1 1 numel(x3)-1]),2,[])'
Related Question