Question: Say we have an array x whose elements represent the boundaries of different bins. Bin 1 is the set of numbers between the first and
Say we have an array x whose elements represent the boundaries of different bins. Bin 1 is the set of numbers between the first and second entries of the x array, Bin 2 is the set of numbers between the second and third entries, and so on. We'd like to write code that takes a scalar input
and returns an index indicating which bin
belongs to.
As an example, suppose we have
binArray = [1, 2.2, 4.1, 5, 7.5, 9]
If we input
, we want the code to return a 2 because 3.8 is in the 2nd bin (it's between
and
). Similarly, if we input
, our code should return a 5, since 8.4 is in the
bin. If
lies on one of the bin boundaries, we'll assign it to the bin on the right. If
lies on the last data point, we'll assign it to the bin on the left. (So
is in bin 2, and
is in bin 5.)
Create a function file to do this. For the bins, we will use an array of randomly generated numbers as indicated in the code outline.
The function starts like this:
function bin = binFind(x0)
rng(2019)
binArray = 20*rand(1,100);
binArray = sort(binArray,'ascend');
can test with this:
%As a test, binFind(15) = 72
binFind(15)
%binFind(pi) = 8
binFind(pi)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
