Error using ismember for two cell arrays

7 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 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.

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签


Translated by