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
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.
Your Function 1 function bin = binFind(x0) 2% Input x0 = a scalar between 0 and 20 3 % Output bin = an integer declaring which bin xe belongs to 4 5 %seeds the random number generator 6% (so we all have the same array) 7 rng (2019) 8% create a 1x100 row vector with "random" numbers from 0 to 20 9 binArray 20*rand (1,100); 10 % sort the values of x, now we have 99 "bins" 11 binArray = sort (binArray, 'ascend'); 14 45 12 13 %run through each bin to check if x0 is in that bin (be careful that there is no bin 1 15 16 17 = 4 C Reset MATLAB Documentation Code to call your function 1%As a test, binFind (15) = 72 2 binFind (15) 3 %binFind(pi) = 8 4 binFind(pi) C Reset
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started