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 Say we have an array x whose elements represent the boundaries of and returns an index indicating which bin different bins. Bin 1 is the set of numbers between the first belongs to.

As an example, suppose we have

binArray = [1, 2.2, 4.1, 5, 7.5, 9]

If we input and second entries of the x array, Bin 2 is the set , we want the code to return a 2 because 3.8 is in the 2nd bin (it's between of numbers between the second and third entries, and so on. We'd and like to write code that takes a scalar input and returns an). Similarly, if we input index indicating which bin belongs to. As an example, suppose we have, our code should return a 5, since 8.4 is in the binArray = [1, 2.2, 4.1, 5, 7.5, 9] If we input , bin. If we want the code to return a 2 because 3.8 is in lies on one of the bin boundaries, we'll assign it to the bin on the right. If the 2nd bin (it's between and ). Similarly, if we input , lies on the last data point, we'll assign it to the bin on the left. (So our code should return a 5, since 8.4 is in the bin. is in bin 2, and If lies on one of the bin boundaries, we'll assign it to 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!