Question

I have a single column matrix A which contain list of times like,

1710
1760
2022
2300
2456
2687
3398
3480
3767
3846
5493
5678
5976
7562
7789
7800
8456
9256
9432
9684
12882
13456
16789

and a cell array B that contains time periods and labels (starttime endtime label)

1708    2392    +
3394    3660    +
5490    5743    +
7555    7809    -
9256    9509    -
12878   15066   -

I want to find the label for each item in A and write that as a new matrix. So I need to check each item in A and compare it with the start time and end time in B and if it belongs to a time limit given there I can write the label (+/-) in matrix C and if it is not there I can write 0 there. How to do this in MATLAB?

Was it helpful?

Solution

you can use bsxfun to compare A to start time and end time in B:

startTime = vertcat( B{:,1} ); % assuming B is 6x3 cell array
endTime = vertcat( B{:,2} );
sel = bsxfun( @ge, A', startTime ) & bsxfun( @le, A', endTime ); % check valid interval
[valid inIdx] = max( sel, [], 1 );

valid is either 1 (for elements in A falling inside one of the intervals in B), or 0. For those elements of A for which valid is 1, the corresponding value of inIdx will give you the interval index (1 to 6).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top