Error using ismember for two cell arrays

15 vues (au cours des 30 derniers jours)
Danielle Leblance
Danielle Leblance le 8 Mai 2018
Commenté : Stephen23 le 8 Mai 2018
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 commentaire
Stephen23
Stephen23 le 8 Mai 2018
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.

Connectez-vous pour commenter.

Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by