MATLAB: How to find a value in an array of different sized cells

cell arraycell arraysfind

I have a cell, S that is a 1×6 cell array of different sized cells. Each of the 6 cells contain different numbers of parts. I am trying to find a way to assign the cell number to the part number. So, if S{1,:} = {1; 2; 3; 4}, S{2,:} ={5; 6; 7}, S{3,:}={8; 9}, etc. I am trying to define a matrix N=[1; 1; 1; 1; 2; 2; 2; 3; 3].
Thank you!

Best Answer

T = arrayfun(@(IDX) {IDX * ones(numel(S{IDX}),1)}, 1:length(S)), 'Uniform', 0);
N = vertcat(T{:});