Conditional retime method in a timetable

2 次查看(过去 30 天)
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.

采纳的回答

Voss
Voss 2024-4-17
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')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by