8eastFromThe3ast
8eastFromThe3ast

Reputation: 197

In matlab, how to calculate elapsed time between rows in a matrix

I have a 21128x9 matrix in the following format:

x = ['Participant No.' 'yyyy' 'mm' 'dd' 'HH' 'MM' 'SS' 'question No.' 'response']

e.g.

   x =

  Columns 1 through 5

      18        2011          10          26          15
      18        2011          10          26          15
      18        2011          10          26          15
      18        2011          10          26          15
      18        2011          10          26          15
      19        2011          10          31          13
      19        2011          10          31          13
      19        2011          10          31          13
      19        2011          10          31          13
      19        2011          10          31          13

  Columns 6 through 9

      42          33          27           4
      42          39          17           2
      42          45          52           2
      42          47          45           3
      42          50          12           3
       6           5          36           1
       6          20          27           4
       6          22          34           5
       6          33          43           3
       6          42          42           1

where columns 2-7 are date vectors. The data are sorted by date/time.

I'd like to calculate the time taken to answer each question for each participant - i.e. the time elapsed between row 1 and 2, 2 and 3, 3 and 4, 4 and 5, and then 6 and 7, 7 and 8 etc. - to end up with a matrix, sorted by participant number, where I can then work out the mean time taken per question.

I've tried using the etime function, but to no avail.

EDIT: With regards to etime, just to see if it would work in practice, I tried to write:

etime(x(2,5:7),x(1,5:7))  

to compare just columns 5-7 of rows 1 and 2, but i keep getting back:

    ??? Index exceeds matrix dimensions.

    Error in ==> etime at 41
    t = 86400*(datenummx(t1(:,1:3)) - datenummx(t0(:,1:3))) + ...

Upvotes: 0

Views: 1611

Answers (2)

Dan
Dan

Reputation: 45762

You were almost there! You needed to change the 5s to 2s, that's all:

etime(x(2,2:7),x(1,2:7))

Now to get them all lets make two matrices of the date vectors but one row out of synch with each other:

fisrt set up x:

 x =[ 18        2011          10          26          15        42          33          27           4
      18        2011          10          26          15        42          39          17           2
      18        2011          10          26          15        42          45          52           2
      18        2011          10          26          15        42          47          45           3
      18        2011          10          26          15        42          50          12           3
      19        2011          10          31          13         6           5          36           1
      19        2011          10          31          13         6          20          27           4
      19        2011          10          31          13         6          22          34           5
      19        2011          10          31          13         6          33          43           3
      19        2011          10          31          13         6          42          42           1]

now extract the times:

Tn = x(1:end-1, 2:7);
Tnplus1 = x(2:end, 2:7);

And no to get a vector of the difference in seconds between consecutive rows:

etime(Tnplus1, Tn)

Which results in:

ans =

           6
           6
           2
           3
      422595
          15
           2
          11
           9

Also if you don't care about the year month day data just set them to zero i.e.

Tn(:, 1:3) = 0;
Tnplus1(:, 1:3) = 0;
etime(Tnplus1, Tn)

ans =

       6
       6
       2
       3
   -9405
      15
       2
      11
       9

Upvotes: 2

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21561

Here are some simple steps:

  • Calculate the difference between the two rows that you want to compare
  • Multiply with a vector that contains the number of seconds per unit

Small scale example:

% Hours Mins Secs:

difference = ([23 12 4] - [23 11 59]);
secvec = difference .* [3600 60 1];
secdiff = sum(secvec)

Upvotes: 1

Related Questions