how to index value falling particular interval

3 views (last 30 days)
I have sequence of numbers [2 2 5 11 15 11 0 7 8 21 27 21 0 1 22 24 24 3 3 13 1 11 13] with min 0 and max 27 value. I want to divide values in intervals of size 4 i.e. 0-3,4-7,8-11,,,,24-27 and want to index particular value in which it falls. i.e. here 7 intervals. 2 falls in 1st interval so index as 1, 5 falls in 2nd interval so index as 2, so as follows
2 2 5 11 15 11 0 7 8....
1 1 2 3 4 3 1 2 3....
how to do it.

Accepted Answer

Guillaume
Guillaume on 22 Dec 2016
v = [2 2 5 11 15 11 0 7 8 21 27 21 0 1 22 24 24 3 3 13 1 11 13];
bins = min(v):4:max(v)+4;
[v; discretize(v, bins)]
  2 Comments
Jay Hanuman
Jay Hanuman on 22 Dec 2016
but interval should be 0-3,5-7,...,23-27, in above code it showing 0-4,5-8,,,,
Guillaume
Guillaume on 22 Dec 2016
The intervals are 0 included to 4 non-included, so [0-4[, 4 included to 8 non-included, so [4-8[, etc. Considering integers, these are thus, 0-3 included, 4-7 included. Exactly as you asked, as can be seen from the output of my code that exactly matches what you asked.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!