Overlapping time interval with points WITHOUT loop

6 vues (au cours des 30 derniers jours)
Alba Solb
Alba Solb le 7 Fév 2020
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 commentaire
Srivardhan Gadila
Srivardhan Gadila le 19 Fév 2020
Are you thinking of using arrayfun in the simplified code?

Connectez-vous pour commenter.

Réponses (1)

Srivardhan Gadila
Srivardhan Gadila le 24 Fév 2020
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by