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

1 回表示 (過去 30 日間)
Mirthand
Mirthand 2021 年 4 月 29 日
コメント済み: Mirthand 2021 年 4 月 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by