Azzera filtri
Azzera filtri

Conditional retime method in a timetable

6 visualizzazioni (ultimi 30 giorni)
Gabi
Gabi il 17 Apr 2024
Commentato: Voss il 18 Apr 2024
Hi,
I'm looking for a way to create a conditional retime (at a fixed fequency, let's say 1 Hz) method based on the actual values of a variable Var in a timetable T.
In the time table T attached::
if Var transitions from 0 -> 1, 0 -> 2, 1 -> 2 or 2 -> 1 use method next,
if Var transitions from 1 -> 0 or 2 -> 0 use method previous.
Is there a way?
Thanks in advance.

Risposta accettata

Voss
Voss il 17 Apr 2024
Maybe something like this:
S = load('timetable.mat');
T2 = S.T2;
dt = 1;
t = seconds(T2.Time);
N = round((t(end)-t(1))/dt)+1;
ti = linspace(t(1),t(end),N).';
Vari = zeros(N,1);
for ii = 1:numel(t)-1
Vari(ti>t(ii) & ti<=t(ii+1)) = T2.Var(ii+logical(T2.Var(ii+1)));
end
% Another way to write that loop; may be easier to understand:
%
% for ii = 1:numel(t)-1
%
% idx = ti>t(ii) & ti<=t(ii+1);
%
% if T2.Var(ii+1) == 0 % transition ends with 0
% % (i.e., 0->0, 1->0, 2->0)
%
% Vari(idx) = T2.Var(ii); % use previous
%
% else % transition ends with 1 or 2
% % (i.e., 0->1, 0->2, 1->1, 1->2, 2->1, 2->2)
%
% Vari(idx) = T2.Var(ii+1); % use next
%
% end
%
% end
T2_retimed = timetable(seconds(ti),Vari, ...
'VariableNames',T2.Properties.VariableNames);
figure
plot(T2.Time,T2.Var,T2_retimed.Time,T2_retimed.Var)
legend({'Original','Retimed'},'Location','NorthWest')

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by