Issue with 'intersect' arrays function
Mostrar comentarios más antiguos
I am trying to intersect two arrays but I keep getting the following error: "Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string".
My arrays look like this:

and

I cannot find the way to intersect them. I have also tried:
[dur, itimes, inewtimes ] = intersect(array2,char(array1));
but no luck.
I attach here the arrays that I am trying to intersect.
Respuestas (2)
You need to convert your times array to strings if you want to compare them. You can't compare a time with a string, whether with intersect or just with a general equality test.
array1 = arrayfun( @(x) char(x), array1, 'UniformOutput', false )
intersect( array2, array1 )
13 comentarios
Nia
el 21 de Ag. de 2015
Sorry, for some reason I put my durations into a cell array when I tested it. I have corrected the code now to use arrayfun instead.
Just using
char( array1 )
will result in a 2-dimensional char array rather than a cell array which is needed for the intersect call according to your error message.
Nia
el 21 de Ag. de 2015
Adam
el 21 de Ag. de 2015
Can you attach a small subset of your sample data? That syntax worked for me when I tried it, but I just created my arrays quickly on the command line with 3 elements in.
Adam
el 21 de Ag. de 2015
Well given the data you have what would you expect it to return? The intersection of those two arrays will be equal to the content of the 2nd array because both those values appear in the other array too.
Nia
el 21 de Ag. de 2015
Adam
el 21 de Ag. de 2015
Ah, ok, so array 2 is not actually strings. I assume they are raw numbers. In which case using the same trick on that array should work. Something like:
array2 = cellfun( @(x) num2str(x), array2, 'UniformOutput', false )
though converting both arrays to strings is probably not the most efficient in that case. Converting the first to a cell array of numeric values array would likely be better if the intersect function supports that.
I was thinking your second array was a cell array of strings.
Nia
el 21 de Ag. de 2015
Nia
el 21 de Ag. de 2015
Steven Lord
el 21 de Ag. de 2015
The reason that you're having so much trouble is that your second input is a cell array of strings. When you call INTERSECT or any of the set functions with one of the inputs being a cell array of strings, both inputs must be cell arrays of strings (or a plain char vector.) Convert that second input into a duration array then you can do what I do in this example. Create two sample duration vectors.
A = seconds(0:5:seconds(hours(1)));
B = minutes(10)+seconds([1 3 5 7 9]);
Intersect them.
[commonDurations, ind1, ind2] = intersect(A, B);
Check the results. To show the results using consistent units, convert B's display format to seconds first. This doesn't change the data stored in B, just the way that data is displayed.
commonDurations
B.Format = 's';
B(ind2)
A(ind1)
1 comentario
Nia
el 21 de Ag. de 2015
Categorías
Más información sobre Cell Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
