I get an erro message when I try this: C = {'one', 'two', 'three'; 1, 2, 3} A = {'one', 'three'; 1, 3} X= intersect(C,A,'rows')

1 visualizzazione (ultimi 30 giorni)
Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string.
  2 Commenti
Image Analyst
Image Analyst il 24 Nov 2015
C =
'one' 'two' 'three'
[ 1] [ 2] [ 3]
A =
'one' 'three'
[ 1] [ 3]
So exactly which do you want to compare? Cell-by-cell? But they're different numbers of cells wide (3 columns vs. 2 columns). Do you want to see if any cell in A lies in any cell of C regardless of which column it finds it in? So A{1,2} = 'three' but it does not occur in the corresponding location, C{1,2}, in cell array C. 'three' occurs in column 2 of A but column 3 of C - do you consider that a match or not?
Do you want to use ismember()????
Ed Robinson
Ed Robinson il 24 Nov 2015
Modificato: Walter Roberson il 25 Nov 2015
oh dear
thanks for your patience
I meant
C = {'one', 1;'two', 2; 'three' , 3}
A = {'one', 1; 'three' , 3}
X= intersect(C,A,'rows')

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 25 Nov 2015
Modificato: Walter Roberson il 25 Nov 2015
"The 'rows' option does not support cell arrays, unless one of the inputs is either a categorical array or a datetime array."
Because of that, you are effectively getting
intersect(C,A)
which is the same as
intersect(C(:), A(:))
and this in turn will fail. You can compare strings to strings and you can compare numbers to numbers, but you cannot have a mix of strings and numbers.
You will find that unique() and ismember() are also not able to handle mixed data types.
[ROW,COL] = ndgrid(1:size(A,1), 1:size(C,1));
match_matrix = arrayfun(@(J,K) isequal(A(J,:),C(K,:)),ROW,COL);
X = C( any(match_matrix & cumsum(match_matrix,2)==1), :);
Note: the output will not be "sorted", and I did not pay attention to whether it is in the order of entries of A or in the order of entries of C. I did, however, take care to remove duplicate matches.
  1 Commento
Ed Robinson
Ed Robinson il 25 Nov 2015
Very grateful thank you. There's a bit to think about there which i will go through. It is also very useful to know that there appears not to be a just simple solution!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion 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!

Translated by