-1

I'm trying to modify a matrix of unix timestamps, into groupings of days.

If I have a matrix like this.

matrix = [1392937181, 1392938000, 1392960000,  1392965000, 1342940000, 1342945000, 1342948000]

I would like to convert it to.

dayMatrix = [1, 1, 2, 2, 3, 3, 3]

Since (1392937181, 1392938000) both are on the first day, (1392960000, 1392965000) occur on a second day and (1342940000, 1342945000, 1342948000) occur on day 3. It is important to keep the number of elements in the matrix the same so that I can plot it against other data.

I know this sounds unusual, but its a requirement asked by a professor for a paper Im writing, but I can`t figure it out.

Any help is appreciated!

Cheers

Dave
  • 3,178
  • 5
  • 28
  • 44
  • 1
    Your first 4 timestamps occur on 20/02/2014 and your last 3 on 22/07/12. How do you get 3 separate days from that? – IKavanagh Oct 26 '15 at 08:30
  • 2
    Possible duplicate of [How to work with Unix timestamps in Matlab?](http://stackoverflow.com/questions/12211710/how-to-work-with-unix-timestamps-in-matlab) – IKavanagh Oct 26 '15 at 08:46
  • Thanks. I accidentally added the wrong time stamp for the second day. Ive edited the question appropriately. And I`ve looked at that SO question and it did not answer my question. – Dave Oct 26 '15 at 08:48

1 Answers1

2

You can covert the unix times to Matlab date vectors a follows:

matrix = [1392937181, 1392938000, 1392939000,  1392940000, 1342940000, 1342945000, 1342948000]

vec = datevec(matrix/86400 + datenum(1970,1,1));

And then to get the days in your format simply subtract the minimum day and add 1:

dayMatrix = vec(:,3) - min(vec(:,3)) + 1

this will output

dayMatrix = [1 1 1 1 3 3 3]
Dan
  • 45,079
  • 17
  • 88
  • 157
  • 1
    Thanks Dan for your quick answer. Sorry about the format error in the question. I accidentally copied some sample data in wrong. Ive updated the question appropriately. – Dave Oct 26 '15 at 09:04