How to check for particular entries in cell arrays

1 回表示 (過去 30 日間)
Mike Chuks
Mike Chuks 2019 年 6 月 20 日
コメント済み: Mike Chuks 2019 年 6 月 20 日
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?

採用された回答

Stephen23
Stephen23 2019 年 6 月 20 日
編集済み: Stephen23 2019 年 6 月 20 日
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
  1 件のコメント
Mike Chuks
Mike Chuks 2019 年 6 月 20 日
Excellent! Thanks a lot Stephen.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStrategy & Logic についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by