MATLAB: Find array inside a cell

find inside cell

I have a A={[1,2,4],[2,3,7],[2,5],[4,5,6]} and B=[1;2;3;6;8]
I want to write a code that find B inside A.
the result should be c={[1,2],[2,3],[2],[6]}.
I try this code
c=cell(1, size(A,2));
for i=1:size(A,2)
current=A{i};
for j=1:size(B,1)
index_X = find(A{i} == B(j));
if isempty(index_X)==0
c{i}(end+1)=B(index_X);
end
end
end
but it does not work

Best Answer

Simpler in one line:
>> A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
>> B = [1;2;3;6;8];
>> C = cellfun(@(m)intersect(m,B),A,'uni',0);
>> celldisp(C)
C{1} =
1
2
C{2} =
2
3
C{3} =
2
C{4} =
6