# Error using ismember for two cell arrays

31 ビュー (過去 30 日間)
Danielle Leblance 2018 年 5 月 8 日
Commented: Stephen Cobeldick 2018 年 5 月 8 日
attached two cell arrays that I am using in my analysis. When I use in a loop:
ismember(StateName2,StateName1(i))
I receive error: Error using cell/ismember Input A of class cell and input B of class cell must be cell arrays of character vectors, unless one is a character vector.
any help is appreciated

#### 1 件のコメント

Stephen Cobeldick 2018 年 5 月 8 日
Both of those cell arrays contain scalar structures in each cell, where all of the structures have the same fields. This is an inefficient way to store that data: it would be much simpler to access if these were non-scalar structures, so the first thing that we will do is convert from these superfluous cell arrays and combine into simpler structure arrays:
>> SN1 = cat(1,StateName1{:});
>> SN2 = cat(1,StateName2{:});
At this point we can see that each non-scalar structure has the same fields:
>> fieldnames(SN1)
ans =
MCOS
string
>> fieldnames(SN2)
ans =
MCOS
string
The string field is always empty:
>> any(~cellfun('isempty',{SN1.string}))
ans = 0
>> any(~cellfun('isempty',{SN2.string}))
ans = 0
Whereas the MCOS field is always a six element column vector:
>> find(~cellfun(@iscolumn,{SN1.MCOS}))
ans = []
>> find(~cellfun(@iscolumn,{SN2.MCOS}))
ans = []
It is not clear from your question how you expect to compare these arrays, but at this point you have several choices, such as using isequal in a loop, or perhaps concatenating into numeric matrices, transposing, and then comparing the rows:
>> mat1 = cat(2,SN1.MCOS).';
>> mat2 = cat(2,SN2.MCOS).';
>> find(ismember(mat2,mat1,'rows'))
ans = []
>> find(ismember(mat1,mat2,'rows'))
ans = []
None of the MCOS field values (i.e. column vectors) exist in the other structure array, so it is not clear what you expect to obtain from this comparison.

サインイン to comment.

### 採用された回答

KSSV 2018 年 5 月 8 日
Try:
s1 = [StateName1{:}]' ;
s2 = [StateName2{:}]' ;
idx = ismember(s2,s1) ;

#### 0 件のコメント

サインイン to comment.