MatLab: Your help is Needed A saddle point of a matrix is as an element whose va
ID: 3768381 • Letter: M
Question
MatLab: Your help is Needed
A saddle point of a matrix is as an element whose value is greater than or equal to every element in its row, but less than or equal to every element in its column. For example, the matrix A = (1 2 3 4 5 6 7 8 9) has only one saddle point and it is in the [1, 3] position. Write a function that finds all the saddle points of a given matrix (note that there may be more than one saddle point!). Your function should return a matrix of indices that has exactly two columns. Each row of this matrix should correspond to one saddle point with the first element of the row containing the row index of the saddle point and the second element containing the column index. If there is no saddle point in M, then your function should return an empty array.Explanation / Answer
function indices = saddle(M)
%Input M is a matrix
%Output indices is a matrix of exactly two columns
%Example indices = saddle([1 2 3;4 5 6;7 8 9])
indices=[];
[m,n]=size(M);
for i=1:n
for j=1:m
a=min(M(:,i)); %minimum element in a column
b=max(M(j,:)); %maximum element in a row
if M(j,i)==a && a==b
indices=[indices;j i];
end
end
end