필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Mirthand
Mirthand 2021년 4월 18일
답변: Prabhan Purwar 2021년 5월 5일
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
Sargondjani
Sargondjani 2021년 4월 18일
It is not clear to me what you want: "I want each trial to start from 0"?
Mirthand
Mirthand 2021년 4월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Prabhan Purwar
Prabhan Purwar 2021년 5월 5일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by