필터 지우기
필터 지우기

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