0

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.

Sarah
  • 325
  • 6
  • 17
  • First: convert the images to actual coded text, makes it easier to actually try your code. Second: since you are using times, using datenum or datevec to convert dates/times to numbers makes comparison a lot easier. – Adriaan Aug 26 '15 at 09:06
  • hmmm, try Array2 = Array2{:}; – GameOfThrows Aug 26 '15 at 09:10
  • @GameOfThrows: If I try Array2=Array2{:}; I get a 1x1 cell array as a result. – Sarah Aug 26 '15 at 09:23
  • @A.Visser: I have applied datenum function to array1 and then I have tried to intersect the arrays but it does not work either:-(. – Sarah Aug 26 '15 at 09:27
  • Your problem is the format for Arrays are different, you want to unify them, you can make everything into CellArrays by cellstr(Array1) cellstr(Array2) and then intersect. – GameOfThrows Aug 26 '15 at 09:34
  • @GameOfThrows: It does not let me to apply cellstr function to array2. I tried cellstr(array2) and I get the following error: 'Input must be a string.' – Sarah Aug 26 '15 at 09:37
  • Oh okay! I see, Array 2 is a Cell of Cells, where each cell has 1 string. – GameOfThrows Aug 26 '15 at 09:41
  • >> whos array2 Name Size Bytes Class Attributes array2 2x1 480 cell – Sarah Aug 26 '15 at 09:44
  • Is there any way to attach my arrays as files in this forum?. It would be easier – Sarah Aug 26 '15 at 09:47
  • I have fixed my answer, you want to do a vertcat(Array2{:,:}) to change Cell of Cells to Cell of Strings. Tricky one, didn't look hard enough at the pictures. – GameOfThrows Aug 26 '15 at 09:47

2 Answers2

1

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.

GameOfThrows
  • 4,510
  • 2
  • 27
  • 44
  • Thanks GameOfThrows. vertcat(array2{:,:}) worked. I did not know that array2 was a cell array of cells where each cell has only 1 string. That was the problem. – Sarah Aug 26 '15 at 10:27
  • @Sarah Yes, I didn't realize that either, the 'String' suggest that it is in a cell, what you had was ' 'String', 'String' ', which could happen sometimes when you are reading in csv or txt files. – GameOfThrows Aug 26 '15 at 10:49
0

Your first array would look something like this:

array1 = linspace(0,1,86400); % creates 86400 seconds in 1 day

Your second array should be converted using datenum, then use cell2mat to make it a matrix. Lastly, use ismember to find the intersection:

InterSect = ismember(array2,array1);
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • If I try: datenum(array2); I get the following error: 'DATENUM failed. The input DATEVEC was not an array of strings'. – Sarah Aug 26 '15 at 09:41
  • The quotationmarks in array2 in your screenshot mark it as being a string. Try cell2mat first, then datenum. – Adriaan Aug 26 '15 at 09:43
  • If I try cell2mat(array2) I now get the following error: 'CELL2MAT does not support cell arrays containing cell arrays or objects.' – Sarah Aug 26 '15 at 10:20