matlab - Comparing content of a cell array with a vector -
i have following 1-by-3 cell arrays:
y = {[2 3 4 5 8],[1 2 5 7 8],[3 4 7 8]}
and following 1-by-8 vector:
x = [1 2 3 4 5 6 7 8]
using form of logical index, compare vector each content of cell array. instance, comparing x y(1,1)
give following:
[0 1 1 1 1 0 0 1]
likewise, comparing x
y(1,2)
give following:
[1 1 0 0 1 0 1 1]
and comparing x
y(1,3)
give following:
[0 0 1 1 0 0 1 1]
hence, should have following output:
[0 1 1 1 1 0 0 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1]
grateful form of help.
use cellfun
apply ismember
each cell's contents:
result = cellfun(@(c) ismember(x, c), y, 'uniformoutput', false); % gives cell array result = vertcat(result{:}); % vertically concatenate cells' contents matrix
Comments
Post a Comment