for loop to subtract every element from the first element for a given trial

1 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2021-4-30
编辑:dpb 2021-4-30
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by