MATLAB Answers

How to restart the time for each trial? (help with a for loop in a table)

2 views (last 30 days)
Mirthand
Mirthand on 18 Apr 2021
Answered: Prabhan Purwar on 5 May 2021
I have a table with two columns, TrialNumber and Minutes that I've attached.
I want each trial to start from 0. I was able to restart the the time for the entire table but not sure how to do it for each trial. Here is what I have so far:
u = unique(tt.TrialNumber);
for k = 1:length(u)
b = find(tt.TrialNumber==u(k));
Mins = (tt{:,2} - tt{1,2});
end
  2 Comments
Mirthand
Mirthand on 18 Apr 2021
15 0
15 0.0263665500000001
15 0.0676836666666669
15 0.0677631000000001
15 0.0678337500000001
15 0.0678979666666670
15 0.0679582166666668
15 0.0680155833333336
15 0.0680712166666670
15 0.0681252833333335
Trial 15 would start like this where instead of starting from 2.6621, it starts from 0.
Then the next trial, 30, the minutes would restart from 0 as well.

Sign in to comment.

Answers (1)

Prabhan Purwar
Prabhan Purwar on 5 May 2021
Hi Mirthand,
Kindly have a look at the following example code using rowfun.
tt.TrialTime=cell2mat(rowfun(@(m)m-m(1),tt,'GroupingVariables','TrialNumber', ...
'InputVariables','Minutes','OutputFormat','cell'));
Output
>>head(tt)
8×3 table
TrialNumber Minutes TrialTime
___________ _______ _________
15 2.594 0
15 2.6203 0.026367
15 2.6616 0.067684
15 2.6617 0.067763
15 2.6618 0.067834
15 2.6619 0.067898
15 2.6619 0.067958
15 2.662 0.068016
>> i30=find(tt.TrialNumber==30);
>> tt(i30:i30+7,:)
8×3 table
TrialNumber Minutes TrialTime
___________ _______ _________
30 5.8881 0
30 5.9285 0.040365
30 5.9602 0.072087
30 5.9603 0.072205
30 5.9604 0.072305
30 5.9605 0.072389
30 5.9606 0.072463
30 5.9607 0.072535
Hope it helps!
Thanks

Community Treasure Hunt

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

Start Hunting!