Q:

MATLAB: dividing a cell's members into smaller cells according to a vector

I have a cell array of size 1*n (X=cell(1, n)). every cell in this cell array(X), has a different size. So, to be clear, here is an example:
X = {X1, X2, X3} (n=3)
X1 = [1 3 5 9]
X2 = [1 2 3 6 7]
X3 = [1 7 8 9]

I have a vector with length of m. for example:
Y = [0 3 6 9] (m=4)

I want to divide cells in my cell array according to this vector in this way:
X1-1=[1] (because 1>=Y(1) & 1<Y(2)), X1-2=[3 5] (because 3 and 5>=Y(2) & 1<Y(3)), X1-3=[9] (because 9>=Y(3) & 9<Y(4))

and so on for X2 and X3...
and finally I have:
Z = {X1-1 X2-1 X3-1
     X1-2 X2-2 X3-2
     X1-3 X2-3 X3-3}

Here is my code with for-loops:
Z = cell(size(X,2), size(Y,2));
for i = 1:size(X,2)
    T = cell2mat(X(i));
    for j = 1:size(Y,2)-1
        idx = (T > Y(j) & T < Y(j+1));
        Z{i,j} = {T(idx)'};
    end
end
Z = cellfun(@(z) cell2mat(z), Z, 'UniformOutput', false);

I want to get the same results without using for-loops. Any vectorization ideas?

A:

You can replace the two for loops with one cellfun and one arrayfun. Not necessarily faster, though:
Z = cellfun(@(Xi) arrayfun(@(k) Xi(Y(k)<=Xi&Xi<Y(k+1)), 1:numel(Y)-1, 'uni', 0).', X, 'uni', 0);

The desired results are stored in Z{1}{1} (corresponding to your X1-1), Z{1}{2} (corresponding to X1-2), etc.
Example: I'm using your data but changing Y to [0 3 6 9.1] (note that with Y = [0 3 6 9] the X1-3 in your example is not correct):
>> Z{1}{1}

ans =

     1

>> Z{1}{2}

ans =

     3     5

>> Z{1}{3}

ans =

     9

>> Z{2}{1}

ans =

     1     2

