필터 지우기
필터 지우기

Conditional retime method in a timetable

조회 수: 6 (최근 30일)
Gabi
Gabi 2024년 4월 17일
댓글: Voss 2024년 4월 18일
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')
  댓글 수: 2
Gabi
Gabi 2024년 4월 18일
Thanks, it surely works.
Voss
Voss 2024년 4월 18일
You're welcome!

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

추가 답변 (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