18.6k views
0 votes
M4: Programming Assignment

My Solutions >
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 nearest among all the elements (recall that we can linearly index a 2D array just like a 1D column array).
A distance between two numbers, say x and y, are measured as the absolute difference between the two Ix - yl.
Note that the qualifying element may not be unique. In such a case, we want to return the index of the first occuring nearest element, that is, the smallest index among the nearest elements. Hint: If 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 second element (65) is the nearest from n with the distance of 160-6515. The 6th element (65) is also the nearest but it's not the first occuring.
n = 25: The fifth element (25) is the nearest from n with the distance of 125-251 = 0.
n=12: The first element (11) is the nearest from n with the distance of 112-1111. The fourth element (13) is also the nearest from n (112 - 131 = 1) but it's not the first occuring. M4: Programming Assignment
My Solutions >
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 nearest among all the elements (recall that we can linearly index a 2D array just like a 1D column array).
A distance between two numbers, say x and y, are measured as the absolute difference between the two Ix - yl.
Note that the qualifying element may not be unique. In such a case, we want to return the index of the first occuring nearest element, that is, the smallest index among the nearest elements. Hint: If 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 second element (65) is the nearest from n with the distance of 160-6515. The 6th element (65) is also the nearest but it's not the first occuring.
n = 25: The fifth element (25) is the nearest from n with the distance of 125-251 = 0.
n=12: The first element (11) is the nearest from n with the distance of 112-1111. The fourth element (13) is also the nearest from n (112 - 131 = 1) but it's not the first occuring.

User Hugh Lin
by
7.9k points

1 Answer

5 votes

Final answer:

The assignment requires searching for the array element closest to a given value 'n' based on absolute difference, and returning the smallest linear index for the first occurrence of this nearest element.

Step-by-step explanation:

The question involves finding the element in a numeric array A that is nearest to a given numeric value n, in terms of absolute difference. This is a programming assignment question likely to be implemented in languages such as Python, MATLAB, or similar. The problem requires computing the minimum distance between the target value n and each element in the array A, and then retrieving both the value of the nearest element and its linear index. In a 2D array, linear indexing means treating the array as if it were a 1D array, where the index increments row-wise first. The specifics of finding the linear index and the nearest value can vary based on the programming language used, but the general approach is to iterate through each element, calculate the absolute difference, and keep track of the smallest distance found as well as the corresponding index and value.

User Xatenev
by
8.2k points