Input A of class cell and input B of class double must be cell arrays of strings, unless one is a string

6 次查看(过去 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
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.
wesso Dadoyan
wesso Dadoyan 2016-5-30
I had data in a table format. I extracted columns A and B. I didn't create the data format. In the original table the double and strings were combined in the column.How can I convert all of them to string?

请先登录,再进行评论。

采纳的回答

Image Analyst
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).

更多回答(1 个)

John BG
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
  1 个评论
wesso Dadoyan
wesso Dadoyan 2016-5-30
thanks john. the format is a mix of double and strings and not only strings. I received the same error
Warning: The 'rows' input is not supported for cell array inputs. > In cellismemberlegacy (line 47) In cell/ismember (line 91) In Step25CorrectionDSIds (line 5) 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.

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by