How to check for particular entries in cell arrays

1 Ansicht (letzte 30 Tage)
Mike Chuks
Mike Chuks am 20 Jun. 2019
Kommentiert: Mike Chuks am 20 Jun. 2019
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?

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Jun. 2019
Bearbeitet: Stephen23 am 20 Jun. 2019
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

Weitere Antworten (0)

Kategorien

Mehr zu Strategy & Logic finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by