Input A of class cell and input B of class double must be cell arrays of strings, unless one is a string
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Image Analyst
2016 年 5 月 30 日
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.
採用された回答
Image Analyst
2016 年 5 月 30 日
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 件のコメント
その他の回答 (1 件)
John BG
2016 年 5 月 30 日
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
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!