Issue with 'intersect' arrays function

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.

Antworten (2)

Adam
Adam am 21 Aug. 2015
Bearbeitet: Adam am 21 Aug. 2015

0 Stimmen

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 Kommentare

Nia
Nia am 21 Aug. 2015
Bearbeitet: Nia am 21 Aug. 2015
I thought I was already doing it by using the char function. But still does not intersect
Nia
Nia am 21 Aug. 2015
@Adam: with your code I now get the following error: "Input #2 expected to be a cell array, was duration instead."
Adam
Adam am 21 Aug. 2015
Bearbeitet: Adam am 21 Aug. 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
Nia am 21 Aug. 2015
Bearbeitet: Nia am 21 Aug. 2015
@Adam: Sorry Adam, I have tested your last code but I keep getting this error: "Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string". I did:
array1 = arrayfun( @(x) char(x), array1, 'UniformOutput', false )
intersect( array2, char(array1) )
Adam
Adam am 21 Aug. 2015
Bearbeitet: Adam am 21 Aug. 2015
You don't want to cast the result of that to a char in the call to intersect.
@Adam: I also tried:
array1 = arrayfun( @(x) char(x), array1, 'UniformOutput', false )
intersect( array2, array1)
but does not work either
Adam
Adam am 21 Aug. 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.
Nia
Nia am 21 Aug. 2015
Bearbeitet: Nia am 21 Aug. 2015
I created the first array in the following way:
A=duration(00,00,00);
B=duration(23,59,59);
array1=linspace(A,B,86400);
and the second array I got it from reading a text file. I do not know how to replicate it as if I create a cell array in the following way:
array={'23:00:22';'23:0023'};
I get the same array2 as before but without quotation marks.
Adam
Adam am 21 Aug. 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
Nia am 21 Aug. 2015
Sorry Adam, that's not the point. What I am trying to say is that if I intersect 'array1' with 'array' your code works. If I intersect 'array1' with 'array2' it does not.
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
Nia am 21 Aug. 2015
for some reason the elements on my array2 appear within quotation marks and that may be the cause of not being able to intersect these two arrays
using:
array2=cellfun( @(x) num2str(x), array2, 'UniformOutput', false )
I get the following error: Undefined function 'abs' for input arguments of type 'cell'

Melden Sie sich an, um zu kommentieren.

Steven Lord
Steven Lord am 21 Aug. 2015

0 Stimmen

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 Kommentar

Nia
Nia am 21 Aug. 2015
@Steven: How can I convert my cell array of strings to a duration array?. I cannot find the function that does that.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

Nia
am 21 Aug. 2015

Bearbeitet:

Nia
am 26 Aug. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by