# How to calculate the difference between different values in a table?

조회 수: 15 (최근 30일)
JCH . 2023년 2월 8일
댓글: Vilém Frynta . 2023년 2월 8일
Hi,
I'm trying to calculate the time differences/lag between event 'ENDSACC' and '1002' (in the message), using the time of each ENDSACC to subtract the time of each 1002.
For example：For the first '1002', its time lag : 'ENDSACC'(2) (72-942= -870) 'ENDSACC'(3) (356-942= -586) 'ENDSACC'(6) (1032-942= 90).
Only the number is important so the ideal result would be like；
-870 -586 90 ...
How can I do such calculation, thanks for any help.

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

### 채택된 답변

Vilém Frynta 2023년 2월 8일
편집: Vilém Frynta 님. 2023년 2월 8일
T = ans; % your table
T_1002 = T(matches(T.message,'1002'),:) % table with only "1002"
T_1002 = 5×5 table
message time code reltime pvel ________ _______ ____ _______ ____________ {'1002'} 3137559 100 942 {0×0 double} {'1002'} 3139829 100 3212 {0×0 double} {'1002'} 3142111 100 5494 {0×0 double} {'1002'} 3143193 100 6576 {0×0 double} {'1002'} 3144087 100 7470 {0×0 double}
T_ENDSACC = T(matches(T.message,'ENDSACC'),:) % table with only "ENDSACC"
T_ENDSACC = 57×5 table
message time code reltime pvel ___________ _______ ____ _______ ____________ {'ENDSACC'} 3136689 400 72 {[192.9000]} {'ENDSACC'} 3136973 400 356 {[248.9000]} {'ENDSACC'} 3137198 400 581 {[130.4000]} {'ENDSACC'} 3137395 400 778 {[248.6000]} {'ENDSACC'} 3137649 400 1032 {[203.8000]} {'ENDSACC'} 3137896 400 1279 {[184.2000]} {'ENDSACC'} 3138104 400 1487 {[205.6000]} {'ENDSACC'} 3138730 400 2113 {[231.9000]} {'ENDSACC'} 3138912 400 2295 {[ 99.5000]} {'ENDSACC'} 3139395 400 2778 {[ 88.4000]} {'ENDSACC'} 3140091 400 3474 {[ 287]} {'ENDSACC'} 3140354 400 3737 {[142.3000]} {'ENDSACC'} 3140683 400 4066 {[ 74.6000]} {'ENDSACC'} 3141321 400 4704 {[276.4000]} {'ENDSACC'} 3141617 400 5000 {[123.2000]} {'ENDSACC'} 3142048 400 5431 {[121.3000]}
% creating matrix before for loop
[x1 ~] = size(T_1002);
[x2 ~] = size(T_ENDSACC);
T_diff = zeros(x1,x2); % will create 5x57 matrix
% doing the calculations and saving them into 5x57 matrix
for q = 1:x1
T_diff(q,:) = int32(T_1002.reltime(q)) - int32(T_ENDSACC.reltime(:));
end
% first row is first1002 vs. endsacc times, second row is second 1002 vs. endsacc times, ...
T_diff
T_diff = 5×57
870 586 361 164 -90 -337 -545 -1171 -1353 -1836 -2532 -2795 -3124 -3762 -4058 -4489 -4788 -5006 -5183 -5366 -5727 -5805 -5899 -6218 -6441 -6706 -6896 -7235 -7468 -9158 3140 2856 2631 2434 2180 1933 1725 1099 917 434 -262 -525 -854 -1492 -1788 -2219 -2518 -2736 -2913 -3096 -3457 -3535 -3629 -3948 -4171 -4436 -4626 -4965 -5198 -6888 5422 5138 4913 4716 4462 4215 4007 3381 3199 2716 2020 1757 1428 790 494 63 -236 -454 -631 -814 -1175 -1253 -1347 -1666 -1889 -2154 -2344 -2683 -2916 -4606 6504 6220 5995 5798 5544 5297 5089 4463 4281 3798 3102 2839 2510 1872 1576 1145 846 628 451 268 -93 -171 -265 -584 -807 -1072 -1262 -1601 -1834 -3524 7398 7114 6889 6692 6438 6191 5983 5357 5175 4692 3996 3733 3404 2766 2470 2039 1740 1522 1345 1162 801 723 629 310 87 -178 -368 -707 -940 -2630
##### 댓글 수: 4이전 댓글 3개 표시이전 댓글 3개 숨기기
Vilém Frynta 2023년 2월 8일
I had a bit of time and I found the fix.
New version of the for loop should work:
for q = 1:x1
T_diff(q,:) = int32(T_1002.reltime(q)) - int32(T_ENDSACC.reltime(:));
end

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

### 카테고리

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

R2022a

### Community Treasure Hunt

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

Start Hunting!