Asked by Muhammad Usman Saleem
on 14 Jun 2015

Hi everyone; I am going to find the saddle points of a matrix M. The question is given below...

Write a function called saddle that finds saddle points in the input matrix M. For the purposes of this problem, a saddle point is defined as an element whose value is greater than or equal to every element in its row, and less than or equal to every element in its column. Note that there may be more than one saddle point in M. Return a matrix indices that has exactly two columns. Each row of indices corresponds to one saddle point with the first element of the row containing the row index of the saddle point and the second column containing the column index. The saddle points are provided in indices in the same order they are located in M according to column-major ordering. If there is no saddle point in M, then indices is the empty array.

I am trying that code:

function indices = saddle(M)

[ rows,cols ] = size(M);

[valR,posR] = max(M,[],2);

[valC,posC] = min(M,[],1);

indices= [];

for i = 1:length(posR)

if i == posC(posR(i))

indices= [indices; i, posR(i)];

end

end

end

It is running fine. But when i test my code for

>> mat=zeros(5,3)

mat =

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

I am getting wrong output :

saddle(mat)

ans =

1 1

the correct output must be

saddle(mat)

ans =

1 1

2 1

3 1

4 1

5 1

1 2

2 2

3 2

4 2

5 2

1 3

2 3

3 3

4 3

5 3

What i am doing wrong?? Thanks in advance

Answer by Stephen Cobeldick
on 15 Jun 2015

Edited by Stephen Cobeldick
on 15 Jun 2015

Accepted Answer

Copying code from the internet is not always a good way to learn best-practice coding: Solving this problem using loops misses using MATLAB's excellent code vectorization abilities. It would be much neater and faster to use bsxfun instead, like this:

function idx = saddle(mat)

row_mx = bsxfun(@ge,mat,max(mat,[],2));

col_mn = bsxfun(@le,mat,min(mat,[],1));

[R,C] = find(row_mx & col_mn);

idx = [R,C];

end

Which gives this:

>> saddle(zeros(5,3))

ans =

1 1

2 1

3 1

4 1

5 1

1 2

2 2

3 2

4 2

5 2

1 3

2 3

3 3

4 3

5 3

Sign in to comment.

Answer by the cyclist
on 14 Jun 2015

The reason your code doesn't give your expected result can be summarized by this sentence from the documentation for max: If the maximum value occurs more than once, then max returns the index corresponding to the first occurrence.

I think you were expecting it to return the indices of all the maxima.

Muhammad Usman Saleem
on 14 Jun 2015

Sign in to comment.

Answer by Konstantinos Sofos
on 14 Jun 2015

Edited by Konstantinos Sofos
on 14 Jun 2015

Dear Muhammad,

You know it's very unfair continuously to ask the forum to solve your exercises/homework. I can understand you because also I was student and I wanted to solve my exercise to proceed but just as a friendly recommendation "Try to understand your exercises!". It's the only way to go one step further without cheating most of all yourself. The code that you posted has been posted before 4 days also to another programming forum Find saddle points in Matlab.

Now to your exercise. Your problem is very simple.

1. Take a piece of white paper and write down a matrix

2. Try to write down an algorithm

3. Write your own function

In my opinion this the way to learn programming. Good luck!

Regards,

Marcos Mariano
on 14 Jun 2015

I am totally agree with Konstantinos Sofos. Look at the forum of your course, you may find the help you need. But I am giving you a hint, try this:

etc..

saddle_mat = [ ];

for jj =

for ii =

if ...

saddle_mat = %add ii jj to the saddle points list as a new row

end

end

end

Muhammad Usman Saleem
on 15 Jun 2015

@Marco thank for your hint

Sign in to comment.

Answer by charu sharma
on 27 Aug 2015

Sign in to comment.

Answer by Jos (10584)
on 3 Apr 2019

function out = saddle(M)

[r, c] = ind2sub(size(M), 1:numel(M)) ;

tf = arrayfun(@(r, c) all(M(r, c) >= A(:, c)) && all(M(r, c) <= M(r, :)), r, c)

out = [r(tf) ; c(tf)].'

Sign in to comment.

Answer by Jaimin Motavar
on 3 Jul 2019

I hope this answer is helpful to you.

function indices = saddle(M)

[m,n]=size(M);

a=[];

for i=1:m

for j=1:n

if prod(M(i,j)>=M(i,:))==1 && prod(M(i,j)<=M(:,j))==1

a=[i,j;a];

end

end

end

indices=a;

end

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Sanket khullr (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/223761-how-to-find-the-element-which-is-greater-than-or-equal-to-its-row-and-smaller-or-equal-to-its-column#comment_689410

## Jack Crespo (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/223761-how-to-find-the-element-which-is-greater-than-or-equal-to-its-row-and-smaller-or-equal-to-its-column#comment_696262

## Xenium Adil (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/223761-how-to-find-the-element-which-is-greater-than-or-equal-to-its-row-and-smaller-or-equal-to-its-column#comment_699973

Sign in to comment.