MATLAB: How to check for particular entries in cell arrays

arrayscell arrays

Hi, I have an N row, single column cell array 'Chn', with each row having different size. Chn is defiend for instance as follows
Chn = {[4,4,3];[2,2,3,3,3];[4,5,2,4,2];[2,3,5,1];1;[4,3];[5,3,2];[3,2,4,5,1];[3,5,1,1];[1,2]};
I also have a vector 'Asg' with its entry ranging between the min. and max. of each rows of 'Chn'. For example, Asg can be defined as
Asg = [3,1];
I want to obtain another cell array, which returns logical results in the rows of Chn for each entry contained in Asg. For the example above, I looking to get the following output;
Res =
{[0,0,1]}
{[0,0,1,1,1]}
{[0,0,0,0,0]}
{[0,1,0,1]}
{1}
{[0,1]}
{[0,1,0]}
{[1,0,0,0,1]}
{[1,0,1,1]}
{[1,0]}
Please, what is a clean way to achieve this?

Best Answer

  • Use ismember within cellfun:
    >> Chn = {[4,4,3];[2,2,3,3,3];[4,5,2,4,2];[2,3,5,1];1;[4,3];[5,3,2];[3,2,4,5,1];[3,5,1,1];[1,2]};
    >> Asg = [3,1];
    >> Res = cellfun(@(m)ismember(m,Asg),Chn,'uni',0);
    >> Res{:}
    ans =
    0 0 1
    ans =
    0 0 1 1 1
    ans =
    0 0 0 0 0
    ans =
    0 1 0 1
    ans =
    1
    ans =
    0 1
    ans =
    0 1 0
    ans =
    1 0 0 0 1
    ans =
    1 0 1 1
    ans =
    1 0