Search code examples
arraysmatlabcelldurationintersect

Array intersection issue (Matlab)


I am trying to carry out the intersection of two arrays in Matlab but I cannot find the way.

The arrays that I want to intersect are:

enter image description here

and

enter image description here

I have tried:[dur, itimes, inewtimes ] = intersect(array2,char(array1)); but no luck.

However, if I try to intersect array1 with array3 (see array3 below), [dur, itimes, inewtimes ] = intersect(array3,char(array1));the intersection is performed without any error.

enter image description here

Why I cannot intersect array1 with array2?, how could I do it?. Thank you.


Solution

  • Just for ease of reading, your formats for Arrays are different, and you want to make them the same. There are many options for you, like @Visser suggested, you could convert the date/time into a long int which allows faster computation, or you can keep them as strings, or even convert them into characters (like what you have done with char(Array2)).

    This is my example:

    A = {'00:00:00';'00:01:01'} %//Type is Cell String
    Z = ['00:00:00';'00:01:01'] %//Type is Cell Char
    Q = {{'00:00:00'};{'00:01:01'}} %//Type is a Cell of Cells
    
    A = cellstr(A) %//Convert CellStr to CellStr is essentially doing nothing
    Z = cellstr(Z) %//Convert CellChar to CellStr
    Q = vertcat(Q{:,:}) %// Convert Cell of Cells to Cell of Strings
    
    I = intersect (A,Z) 
    
    >>'00:00:00'
      '00:01:01'
    
    II = intersect (A,Q)
    >>'00:00:00'
      '00:01:01'    
    

    This keeps your dates in the format of Strings in case you want to export them back into a txt/csv file.