How to check ismember in a cell in Matlab?

66 Ansichten (letzte 30 Tage)
SM
SM am 24 Okt. 2019
Beantwortet: SM am 24 Okt. 2019
List={[5,2] [27,19] [13,8] [17,14] [3,13] [21,20] [6,16] [4,30] [1,13] [20,2] [14,27];
[5,2] [21,7] [29,6] [17,36] [35,12] [8,20] [35,8] [11,10] [35,23] [22,25] [9,32]};
B={[21 20]};
A=ismember(List{1,:}, B) % it will only check the row of the cell List.
result=[0 0 0 0 0 1 0 0 0 0 0]
is it possible?

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Okt. 2019
>> List = {[4,5,2],[27,19],[13,8],[17,14],[3,13],[21,20],[6,16],[4,30],[1,13],[20,2],[14,27,2];[5,2],[21,7],[29,6],[17,36],[35,12],[8,20],[35,8],[11,10],[35,23],[22,25],[9,32]};
>> B = {[21,20]}; % why the superfluous cell array?
>> X = cellfun(@(m)isequal(m,B{1}),List(1,:))
X =
0 0 0 0 0 1 0 0 0 0 0

Weitere Antworten (2)

Andrei Bobrov
Andrei Bobrov am 24 Okt. 2019
result = cellfun(@(x)all(x == B{:}),List);
  1 Kommentar
SM
SM am 24 Okt. 2019
Excellent! But It must be only check the first row. Another issue is that if the first row is replaced by [4, 5,2] [27,19] [13,8] [17,14] [3,13] [21,20] [6,16] [4,30] [1,13] [20,2] [14,27,2]; then it shows error 'Matrix dimensions must agree'. Is it possible to solve now?

Melden Sie sich an, um zu kommentieren.


SM
SM am 24 Okt. 2019
Thank you! It works.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by