for loop to subtract every element from the first element for a given trial
2 views (last 30 days)
Show older comments
I have two columns with TrialNumber and Minutes in the table tt (please see attached).
I'm trying to get the Minutes to start from 0 for each of the trials. To do this, I am trying to subtract every element from the first element in a trial and that will restart time for each trial and have each trial start from 0.
I've attempted this with a for loop but it doesn't seem to be working:
allTrials = unique(tt.TrialNumber);
for ii = 1:length(allTrials)
bb = find(tt.TrialNumber == allTrials(ii));
for jj = 2:length(bb)
nn(jj) = tt.Minutes(bb(jj)) - tt.Minutes(bb(1));
end
end
0 Comments
Accepted Answer
dpb
on 30 Apr 2021
Edited: dpb
on 30 Apr 2021
Another job for varfun and grouping variables--
tt.TrialTime=cell2mat(rowfun(@(m)m-m(1),tt,'GroupingVariables','TrialNumber', ...
'InputVariables','Minutes','OutputFormat','cell'));
results in
>> head(tt)
ans =
8×3 table
TrialNumber Minutes TrialTime
___________ _______ _________
15.00 7.79 0.00
15.00 7.84 0.05
15.00 7.86 0.07
15.00 8.17 0.38
15.00 8.17 0.38
15.00 8.17 0.38
15.00 8.17 0.38
15.00 8.17 0.38
>>
To illustrate it "did the right thing" overall,
>> i30=find(tt.TrialNumber==30);
>> tt(i30:i30+7,:)
ans =
8×3 table
TrialNumber Minutes TrialTime
___________ _______ _________
30.00 16.15 0.00
30.00 16.16 0.01
30.00 16.36 0.21
30.00 16.66 0.51
30.00 16.66 0.51
30.00 16.66 0.51
30.00 16.66 0.51
30.00 16.66 0.51
>>
The dataset appears to be short of precision in the measured time -- many times are not distinguishable.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!