Do you need to use array cells to save space?
Otherwise, you can change your current matrix to the normal matrix MxN, where N is n, as you defined, and M is the number of groups. And then just strip the end of each line with zeros. Thus, it contains the same information, but makes your return request simple using find .
therefore, if n = [1 2 3 4 5 6 7]
and we have 3 groups for which group 1 [1 4 5] , group 2 is [3] , and group 3 is [1 2 6 7] , your current matrix will be
M = 3; N = numel(n); m = zeros(M,N); m(1, 1:3) = [1 4 5]; m(2, 1) = 3; m(3, 1:4) = [1 2 6 7];
Now you want to know which group the number i belongs to. It's that simple (updated based on Chris Taylor's observation)
find(any(m == i, 2))
source share