Overlapping time interval with points WITHOUT loop

조회 수: 7 (최근 30일)
Alba Solb
Alba Solb 2020년 2월 7일
답변: Srivardhan Gadila 2020년 2월 24일
I would appreciate and help to some simple code to find this without a loop:
startBin = [1 10 25 37];
endBin = [20 30 40 45];
points = [2 11 19 26 35 41 43];
a representation:
1---------------------20 (A)
10 ------------------------30 (B)
25-----------------------40 (C )
37-----------------45 (D)
2 11 19 26 35 41 43
  • point 2 is in interval A
  • point 11 is in interval A and B
  • point 19 is in interval A and B
  • point 26 is in interval B and C
  • point 35 is in interval C
  • point 41 is interval D
  • point 43 is interval D
The output I am looking for is this, where for each point I have the index of which interval:
output =
1 1 % point 2 is within interval 1-20
2 1 % point 11 is within interval 1-20
2 2 % point 11 is within interval 10-30
3 1 % point 19 is within interval 1-20
3 2 % point 19 is within interval 10-30
4 2 % point 26 is within interval 10-30
4 3 % point 26 is within interval 25-40
5 3 % point 35 is within interval 25-40
6 4 % point 41 is within interval 37-45
7 4 % point 43 is within interval 37-45
This is a very simplified version of what I have to do, because I am looking at intervals of time and if point falls within this interval, then I calculated standard deviation, etc. The matrix I am working with is of 3million intervals, this is why I want to simplifieid without having to use parfor, or spmd.
I have seen using linear indexing and cumsum to solve this problem, however, I can figure out how to do it if I not comparing two intervals that do not have the same size.
I would appreciate any help.
Thank you so much.

답변 (1개)

Srivardhan Gadila
Srivardhan Gadila 2020년 2월 24일
If you are thinking of using arrayfun, then the following code might help you,
startBin = [1 10 25 37];
endBin = [20 30 40 45];
points = [2 11 19 26 35 41 43];
inds = 1:numel(points);
binds = 1:numel(startBin);
res = arrayfun(@(points,inds) func(startBin,endBin,binds,points,inds),points,inds,'UniformOutput',0);
rr = cell2mat(res);
rr'
function out = func(startBin,endBin,binds,pt,ind)
out = arrayfun(@(startBin,endBin,binds) findd(startBin,endBin,binds,ind,pt),startBin,endBin,binds,'UniformOutput',0);
out = cell2mat(out);
end
function out = findd(st,en,bind,ind,pt)
out = [];
if (pt>=st) && (pt<=en)
out = [ind bind]';
end
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by