필터 지우기
필터 지우기

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 (최근 30일)
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

답변 (2개)

alice
alice 2017년 6월 22일
편집: alice 2017년 6월 22일
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 2017년 6월 22일
편집: Jan 2017년 6월 22일
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)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by