Use matlab to answer the following question in the picturebelow. Beneath this statement is an example. Thank
Question:
Use matlab to answer the following question in the picturebelow. Beneath this statement is an example. Thank you :)
Say we have an array x whose elements represent the boundariesof different bins. Bin 1 is the set of numbers between the firstand second entries of the x array, Bin 2 is the set of numbersbetween the second and third entries, and so on. We'd like to writecode that takes a scalar input x0 and returns an index indicatingwhich bin x0 belongs to.
As an example, suppose we have
binArray = [1, 2.2, 4.1, 5, 7.5, 9]
If we input x0 = 3.8, we want the code to return a 2 because 3.8is in the 2nd bin (it's between x(2) and x(3) ). Similarly, if weinput 8.4, our code should return a 5, since 8.4 is in the 5th bin.If x0 lies on one of the bin boundaries, we'll assign it to the binon the right. If x0 lies on the last data point, we'll assign it tothe bin on the left. (So x0 = 2.2 is in bin 2, and x0 = 9 is in bin5.)
Create a function file to do this. For the bins, we will use anarray of randomly generated numbers as indicated in the codeoutline.
This problem seems a bit esoteric (that's for those of you whoread), but this process will actually be important when we dosomething called cubic spline interpolation. Since the context ifforthcoming, we'll pseudocode this one for you. Here is the thoughtprocess:
Let's say x0 = 6.7 using the binArray above. I know thatbin = 4 because x0 is bigger than binArray(4) butsmaller than binArray(5). So, when x0 is smaller thanbinArray(bin+1), it should be in bin 'bin' . We want Matlab tofigure that out.
I'll start by guessing that
bin = 1
and check: is x0>binArray(bin+1)? At this step, x0= 6.7 is bigger than binArray(bin+1) = 2.2, so we guess again:
bin = 2
Check again: Is x0>binArray(bin)? At this step, x0=6.7 is bigger than binArray(bin+1) = 4.1, so we guessagain, and Check again until x0 is bigger thanbinArray(bin+1) and we conclude we want bin 4.
Notice that to do this, we repeated two tasks (update , andcheck an inequality). How many times did we repeat the tasks? Willthis be the same for every input ? What kind of loop is this?Finally, doing this process will work for any that is between twovalues in . What about the edges of the bins? What about the veryfar right edge? You will need to consider how these points will betreated under this process. Remember that you can use multipleconditions in the same loop.
Intermediate Accounting
ISBN: 978-0132162302
1st edition
Authors: Elizabeth A. Gordon, Jana S. Raedy, Alexander J. Sannella