How to simply the code
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Riccardo Tronconi
am 20 Jul. 2021
Beantwortet: Peter Perkins
am 27 Jul. 2021
Dear guys I have addressed this code but I would appreciate to know if there is a more efficient way to compute this operation.
I have C = mx2 as input data in which:
Column 1 Column 2
'05-Mar-2021 11:41:37.000000000' 1.82586529734179
'05-Mar-2021 11:41:38.000000000' 1.77388482137334
'05-Mar-2021 11:41:39.000000000' 1.88714319565667
'05-Mar-2021 11:41:40.000000000' 2.29089559960008
'05-Mar-2021 11:41:41.000000000' 2.30346664313351
'05-Mar-2021 11:41:42.000000000' 1.87713583687103
'05-Mar-2021 11:41:43.000000000' 1.76028809274914
'05-Mar-2021 11:41:44.000000000' 2.29089559960008
'05-Mar-2021 11:41:45.000000000' 1.91069619238497
'05-Mar-2021 11:41:46.000000000' 1.87713583687103
'05-Mar-2021 11:41:47.000000000' 1.70193502387195
'05-Mar-2021 11:41:48.000000000' 1.95785425847213
'05-Mar-2021 11:41:49.000000000' 1.87823181906136
I loop over each row and I stay inside a while loop if the value in the second column is lower than 2. Once, it is greater than the threshold I exit and it is computed the difference between the first timestamp to the last one to find the duration.
is it possibile to simply the code?
2 Kommentare
Akzeptierte Antwort
Jan
am 20 Jul. 2021
Bearbeitet: Jan
am 20 Jul. 2021
With some guessing:
Value = cat(1, C{:, 2});
Index = find(Value >= 2, 1, 'first') - 1;
if ~isempty(Index) % [EDITED]
Time1 = C{1, 1};
Time2 = C{index, 1};
Duration = datetime(Time2) - datetime(Time1)
else
% What do you want to do, if no element matchs?
end
4 Kommentare
Jan
am 20 Jul. 2021
@Riccardo Tronconi: I've inserted a check, if no value is found.
The shown procedure should work for an arbitrary number of rows.
Weitere Antworten (1)
Peter Perkins
am 27 Jul. 2021
"I'm working with cell arrays": Riccardo, you don't want to be doing that, especially not with 100000 rows. Or using a loop for that matter.
I think the following does what you want, though I'm not actually 100% sure what that is.
>> t = datetime({'05-Mar-2021 11:41:37.000000000'
'05-Mar-2021 11:41:38.000000000'
'05-Mar-2021 11:41:39.000000000'
'05-Mar-2021 11:41:40.000000000'
'05-Mar-2021 11:41:41.000000000'
'05-Mar-2021 11:41:42.000000000'
'05-Mar-2021 11:41:43.000000000'
'05-Mar-2021 11:41:44.000000000'
'05-Mar-2021 11:41:45.000000000'
'05-Mar-2021 11:41:46.000000000'
'05-Mar-2021 11:41:47.000000000'
'05-Mar-2021 11:41:48.000000000'
'05-Mar-2021 11:41:49.000000000'});
>> x = [1.82586529734179
1.77388482137334
1.88714319565667
2.29089559960008
2.30346664313351
1.87713583687103
1.76028809274914
2.29089559960008
1.91069619238497
1.87713583687103
1.70193502387195
1.95785425847213
1.87823181906136];
>> t = table(t,x)
t =
13×2 table
t x
____________________ ______
05-Mar-2021 11:41:37 1.8259
05-Mar-2021 11:41:38 1.7739
05-Mar-2021 11:41:39 1.8871
05-Mar-2021 11:41:40 2.2909
05-Mar-2021 11:41:41 2.3035
05-Mar-2021 11:41:42 1.8771
05-Mar-2021 11:41:43 1.7603
05-Mar-2021 11:41:44 2.2909
05-Mar-2021 11:41:45 1.9107
05-Mar-2021 11:41:46 1.8771
05-Mar-2021 11:41:47 1.7019
05-Mar-2021 11:41:48 1.9579
05-Mar-2021 11:41:49 1.8782
>> t.run = cumsum(x >= 2)
t =
13×3 table
t x run
____________________ ______ ___
05-Mar-2021 11:41:37 1.8259 0
05-Mar-2021 11:41:38 1.7739 0
05-Mar-2021 11:41:39 1.8871 0
05-Mar-2021 11:41:40 2.2909 1
05-Mar-2021 11:41:41 2.3035 2
05-Mar-2021 11:41:42 1.8771 2
05-Mar-2021 11:41:43 1.7603 2
05-Mar-2021 11:41:44 2.2909 3
05-Mar-2021 11:41:45 1.9107 3
05-Mar-2021 11:41:46 1.8771 3
05-Mar-2021 11:41:47 1.7019 3
05-Mar-2021 11:41:48 1.9579 3
05-Mar-2021 11:41:49 1.8782 3
>> rowfun(@(t,x)t(end) - t(1), t, "InputVariables","t","GroupingVariables","run")
ans =
4×3 table
run GroupCount Var3
____ __________ ________
0 3 00:00:02
1 1 00:00:00
2 3 00:00:02
3 6 00:00:05
I've used a table, not a timetable because rowfun doesn't currently let you use the row times as a data variable.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Numeric Types finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!