How can I create a list of ranges and tell the program to verify in which range is my number and pick the inferior limit?

2 visualizaciones (últimos 30 días)
Hi, I have a quite simple question but It's difficult to me to translate that in Matlab code. I have a vector of numbers like a= [1,3,6,12,24,35] and a list of ranges that I divided in two vectors with all the inferior limits and the superior limits like: inf=[0,5,10,15,20,25,30] and sup=[5,10,15,20,25,30].
My goal it to verify in which range stays a number of the vector a and to create a vector of the inf limits of the range.
For example: first number =1, it stays in the range 0-3 so the answer must be 0. second number =3, it stays in the same range so the answer must be another time 0.
The same thing for every number of the vector a.
Thanks for the help,
Federico

Respuestas (2)

alice
alice el 22 de Jun. de 2017
Editada: alice el 22 de Jun. de 2017
Hi,
  • In Matlab, inf is the keyword for the infinity so you should avoid naming your inferior limits inf. Maybe you can name your vectors inf_lim and sup_lim ?
  • One way to find the inferior limit of the interval in which a value is:
inf_lim=[0,5,10,15,20,25,30]; % inferior limits
sup_lim=[5,10,15,20,25,30,Inf]; % superior limits (I add the infinity in order to have the same number of elements)
value = 12; % value to be tested
inf_lim((value>inf_lim) & (value<sup_lim)) % the answer
  • In your case, the simplest way to do it is to notice your inferior limits are all the multiple of 5 up to 30:
a=[1,3,6,12,24,35];
inf_limits = min(30,5*floor(a/5));

Jan
Jan el 22 de Jun. de 2017
Editada: Jan el 22 de Jun. de 2017
a = [1,3,6,12,24,35]
edge = [0,5,10,15,20,25,30,Inf];
[N, edge, bin] = histcounts(a, edge);
Now bin tells you the interval a given number fells in and edge(bin) should be the wanted anser.
Or:
bin = discretize(a, edge);
edge(bin)

Categorías

Más información sobre Graphics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by