Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
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 Kommentare
Image Analyst
Image Analyst am 30 Mai 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.
wesso Dadoyan
wesso Dadoyan am 30 Mai 2016
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (1)

John BG
John BG am 30 Mai 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
  1 Kommentar
wesso Dadoyan
wesso Dadoyan am 30 Mai 2016
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by