Programming Test 3 matlab (zyBooks)
Programming Test 3 Problem Given a numeric array A and a numeric value n, find the linear Index AND the value of A's element whose distance from n is farthest among all the elements (recall that we can linearly index a 2D array just like a 10 column array) A distance between two numbers, say x and y, are measured as the absolute difference between the two lx - yl. Note that the qualifying element may not be unique. In such a case, we want to return the index of the last occuring farthest element, that is the largest index among the farthest elements. Hint, you coded if-statement correctly, then you don't really need to worry about this at all Examples Consider the 3x3 array(11, 13, 18;65, 25, 31:49, 65, 37). . n = 60: The first element (11) is the farthest from n with the distance of 160-111 = 49. . n = 30: The 6th element (65) is the farthest from n with the distance of 130.651 - 35. The second element (65) is also the farthest but it's not the last occuring . n = 38. The 6th element (65) is the farthest from n with the distance of 138 - 65127. The first and second elements (11 and 65) are also the farthest from n (138 - 111 = 27) but they are not the last occuring Task Write a function farthestElement that takes two input arguments in this order an array to be searched and the search key) and returns two output values in this order an index of the last occurring tarthest element of the input array and its value)
Grading You are allowed unlimited submission while the test is available. However, the automatic grader will update Canvas with your last submission, NOT your best submission. Ensure that there is enough time to either fix any minor syntax error in your last attempt or revert back to previous best effort solution. Use of the built-in function "max" or other similar built-in functions will result in not passing the automatic assessment Hints • Don't be fooled by the example 2D array. This problem does not need a nested loop as long as you treat the 2D array like a 10 array with linear indexing. For example, given a 2D array M, what does Mc) give you? • Take time to understand the problem and analyze it. You should notice that this is actually a familiar problem in a disguise. If you take the element-wise difference between A and n, the resulting array should only consists of distances. What would you call the elements of the distance array that corresponds to the farthest elements in A? • Remember, "The server timed out while running... most likely means that you have an infinite loop in your code a
Function Save Reset MATLAB Documentation 1 Modify the function header below 2 function CHANGE_THIS_HEADER 3 * YOUR CODE STARTS 4 5 YOUR CODE ENDS 6 end Code to call your function e Roset 1 inputarr = (11, 13, 18; 65, 25, 31; 49, 65, 37): 2 [idxShouldBel, valShouldBe11] = farthestElement(inputArr, 60) 3 [idxShouldBe6, valShouldBe65] = farthestElement(inputArr, 30) 4 [idxShouldBe6, valShouldBe65] = farthestElement(inputArr, 38)
Assessment: Submit Check the function definition Check whether the function returns correct index (output arg 1) for the example cases Check whether the function returns correct element (output arg 2) for the example cases Check whether the function returns correct index (output arg 1) for one-element arrays Check whether the function returns correct element (output arg 2) for one- element arrays Check whether the function returns correct index (output arg 1) for random arrays Check whether the function returns correct index (output arg 2) for random arrays Check whether the function returns correct index (output arg 1) for arrays with guaranteed non-unique qualifying elements Check whether the function returns correct element (output arg 2) for arrays with guaranteed non-unique qualifying elements