ismember for cell arrays of different sizes

7 Ansichten (letzte 30 Tage)
Sean McWeeny
Sean McWeeny am 6 Okt. 2022
Beantwortet: Fangjun Jiang am 6 Okt. 2022
I have a cell array with different sized cells of class double as follows:
bin_list = {[1], [1,7], [1,7], [1,7,9], [2,8], [3], [1,7]}';
I would like to count all the cells that have a 1 in them (eventually nest it within a for loop such that I count all the cells that have a 2,3, and n in them)

Akzeptierte Antwort

Davide Masiello
Davide Masiello am 6 Okt. 2022
bin_list = {[1], [1,7], [1,7], [1,7,9], [2,8], [3], [1,7]}'
bin_list = 7×1 cell array
{[ 1]} {[ 1 7]} {[ 1 7]} {[1 7 9]} {[ 2 8]} {[ 3]} {[ 1 7]}
cellfun(@(x)ismember(1,x),bin_list)
ans = 7×1 logical array
1 1 1 1 0 0 1

Weitere Antworten (1)

Fangjun Jiang
Fangjun Jiang am 6 Okt. 2022
bin_list = {[1], [1,7], [1,7], [1,7,9], [2,8], [3], [1,7]}';
Const=1;
f=@(x) ismember(Const,x);
cellfun(f,bin_list)
ans = 7×1 logical array
1 1 1 1 0 0 1

Kategorien

Mehr zu Data Types 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