Isolating segments of data in one column using time integrals from the next column
1 次查看(过去 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.
0 个评论
采纳的回答
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]
>>
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!