Input A of class cell and input B of class double must be cell arrays of strings, unless one is a string
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
wesso Dadoyan
il 30 Mag 2016
Risposto: Image Analyst
il 30 Mag 2016
A=
89830410
50590220
'33762X10'
'02837P30'
57832110
'25037Y10'
13063010
'09972F10'
B=
89830410
50590220
'33762X10'
for i=1:length(B)
x0=find(ismember(A,B{i}));
end
is giving me error.
Error using cell/ismember (line 34)
Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string.
How can i match A to B(i)without error.
4 Commenti
Image Analyst
il 30 Mag 2016
Tell us why A is not ALL strings. Why are there doubles/scalars in there (rows 1,2,5 and 7)? Maybe you can convert then to strings and then do the ismember.
Risposta accettata
Image Analyst
il 30 Mag 2016
You can get a matrix of what element of A matches what element of B using isequal() and iterating over all possible comparisons/pairs.
A= {...
89830410
50590220
'33762X10'
'02837P30'
57832110
'25037Y10'
13063010
'09972F10'}
B={...
89830410
50590220
'33762X10'}
lenA = length(A);
lenB = length(B);
matches = false(lenA, lenB);
for ka = 1 : lenA
for kb = 1 : lenB
if isequal(A(ka), B(kb))
matches(ka, kb) = true;
end
end
end
% Print to command window:
matches
Results:
matches =
1 0 0
0 1 0
0 0 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
This will work regardless if the contents of the cells of A and B are strings or numbers. The output matches matrix is basically saying that A(1) matches B(1), A(2) matches B(2), and A(3) matches B(3).
0 Commenti
Più risposte (1)
John BG
il 30 Mag 2016
Mr Dadoyan
please check if you find the following useful:
A= [
'89830410';
'50590220';
'33762X10';
'02837P30';
'57832110';
'25037Y10';
'13063010';
'09972F10';]
B=['89830410';
'50590220';
'33762X10';]
x0=ismember(A,B,'rows')
=
1
1
1
0
0
0
0
0
there is no need for a loop if you use the option 'rows' in ismember.
To get the common elements
(find(x0>0),:)
ans =
89830410
50590220
33762X10
If you find this answer of any help solving your question,
please click on the thumbs-up vote link, or mark it as accepted answer
thanks in advance
John
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!