Here is a simple approach. Note I define a saddle point as one that is either the largest in its column and smallest in its row or the smallest in its column and largest in its row. Maybe you only want to look for the second kind in which case you can modify the approach accordingly.
Also this code is quite inefficient. You could further optimize it by finding and saving the column maximums, column minimums, row maximums and row minimums before entering the loop.
You could also probably vectorize this further and not use a loop at all, but I think you wanted to see how the basic syntax would look.
A = [10 12 7 3 12;
3 10 6 2 8;
12 24 17 6 10;
15 21 10 8 12;
1 18 22 4 15];
disp A
[numRows,numCols] = size(A);
indices = zeros(2,2);
numSaddle = 0;
for iRow = 1:numRows
for jCol = 1:numCols
brsc = A(iRow,jCol) == max(A(iRow,:)) && A(iRow,jCol) == min(A(:,jCol));
srbc = A(iRow,jCol) == min(A(iRow,:)) && A(iRow,jCol)== max(A(:,jCol));
if brsc || srbc
numSaddle = numSaddle + 1;
indices(numSaddle,:) = [iRow,jCol];
end
end
end
indices = indices(1:numSaddle,:);
disp(indices)
for k = 1:numSaddle
disp(A(indices(k,1),indices(k,2)))
end
Best Answer