Isolating segments of data in one column using time integrals from the next column

조회 수: 2 (최근 30일)
Hi,
I am trying to find segments of data from intervals of start and stop times. The data is a matrix with data in the first column and time stamps in the second column that correspond to the interval times. I would like to get the data in the first column that corresponds to the time stamps in the second column. I would also like the out put interval (there are multiple intervals, in the samples I provided there are 3) to be that each segment is in a separate matrix row or column or even cell array (any is find I just want them to be separate). I have tried some code but am getting empty cell array outputs:
for i=1:numel(interval(:,1))
w=interval(i,:);
output{i}=intersect(data(find(data>=w(1))),data(find(data<=w(2))));
end
I have attached an example of data in a matrix data and intervals in matrix interval. Any help would be wonderful.

채택된 답변

dpb
dpb 2017년 6월 12일
Logical addressing to the rescue!!! :)
>> for i=1:length(interval)
c{i}=data(iswithin(data(:,2),interval(1,1),interval(1,2)),:);
end
>> whos c
Name Size Bytes Class Attributes
c 1x3 336180 cell
>> c{1}(1:5,:)
ans =
-0.1134 62.6188
-0.1084 62.6198
-0.1103 62.6208
-0.0544 62.6218
-0.0191 62.6228
>>
iswithin is my "syntactic sugar" utility routine--
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
You can collapse the loop via arrayfun; whether it's any faster or not is probably iff--
>> arrayfun(@(i1,i2) data(iswithin(data(:,2),i1,i2),:), ...
interval(:,1),interval(:,2), ...
'uniformoutput',0)
ans =
[7000x2 double]
[7000x2 double]
[7000x2 double]
>>
  댓글 수: 1
Krispy Scripts
Krispy Scripts 2017년 6월 13일
Thank you!! Yes, I figured I need to use logical addressing, but was having problems using it to find corresponding values in a matrix

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by