Write a function find_2D_max that finds the maximum element in a 2 - dimensional
ID: 3775467 • Letter: W
Question
Write a function find_2D_max that finds the maximum element in a 2 - dimensional array and returns its indices. Your functions should take one input argument, a 2 - D array and return three output arguments: the maximum element, its row index, and its column index (attention: not to pri nt or display, but to return three output arguments). For simplicity, you may assume that the maximum element is unique. You cannot use any built - in functions that find the maximum element and its indices. You have to implement a loop (nested loop). You ma y want to use the built - in function size . If the input array is empty or not an array of real numbers, return three NaN values.
Example:
>>
A = [13,4,3,6;11,9,1,15;5,14,16,8;10,2,7,12]
A=
13 4 3 6
11 9 1 15
5 1416 8
10 2 7 12
>> [maxEl,row,column] = find_2D_max(A)
maxEl =
16
row =
3
column =
3.
Has to be a matlab function.
Explanation / Answer
function [max,row,col] = find_2D_max(A)
max=A(1,1)
row=1
col=1
[r,c]=size(A)
for i=1:r+1
for j=1:c+1
if(A(i,j)>max)
max=A(i,j)
row=i
col=j
end
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.