extract values from a vector for 5 secs every time

1 次查看(过去 30 天)
i have 2 vectors. Time (sec) = [1 2 3 4 5 6 7 ...... 1000] and Alarm_active =[0 0 1 1 1 1 0 0 0 1 1 1 0..etc]. basically when Alarm is active it reads 1 and when it goes off it reads 0. i want to create a new vector from the Alarm_active which only accounts for Alarm_active for 5 seconds only. i.e. if Alarm went active(1) at time=240 sec and remained active until time=265 secs, then my new vector should only report Alarm_active (1) from time=240 to 245 sec and then go to 0 until 265 sec. it should check this every time the Alarm_active goes to 1
  4 个评论
Amit Pandey
Amit Pandey 2018-10-26
sorry Kevin, if i am not clear. let me give an example to my question - time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] Alarm_active =[1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1] so i want to create a vector which account for only 2 sec of Alarm_active. so my new vector (Alarm_active_2sec) would be - Alarm_active_2sec =[1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0]; so when ever Alarm_active goes 1, alarm_active_2sec goes 1 for only 2 sec. hope i make myself clear. thanks for your response.

请先登录,再进行评论。

回答(2 个)

KSSV
KSSV 2018-10-26
t = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] ;
A =[1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1] ;
% Resahpe for evry two secs
t = reshape(t,2,[])' ;
A = reshape(A,2,[])' ;
% GEt alaram active for 2 secs
AC = sum(A,2)
idx = AC~=0 ;
t_active = t(idx)

Kevin Chng
Kevin Chng 2018-10-26
编辑:Kevin Chng 2018-10-26
Hi Amit Pandrey,
if let say you want to get the result of every 2 seconds from Alarm_active.
time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
Alarm_active =[1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1];
timeinterval = 2 %2seconds;
t= time(1:timeinterval:end);
a= Alarm_active(1:timeinterval:end);
result = table(t',a');
result.Properties.VariableNames = {'Time','Alarm_Condition'}
if let say you don't want start from 1st second, want to start from 2nd second, then change 1 to 2.
t= time(2:timeinterval:end);
a= Alarm_active(2:timeinterval:end);

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by