# how to find the element which is greater than or equal to its row and smaller or equal to its column in a matrix

1.552 views (last 30 days)
Muhammad Usman Saleem on 14 Jun 2015
Commented: Avinav Ayushman on 8 Aug 2020 at 8:31
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:
[ 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 :
ans =
1 1
the correct output must be
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

Xenium Adil on 30 Apr 2019
hey budies i am still stuck at sparse2matrix and have almost completed the course help me out solving that......... trying this code
function [matrix]=sparse2matrix(incell)
S=size(incell);
q=S(2)-2;
msize=incell{1};
mdef=incell{2};
matrix=repmat(mdef,msize);
while q>0
matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);
q = q-1;
end
but failing for random cases help me out...
Sri Sai Nomula on 21 Apr 2020
function matrix=sparse2matrix(cellvec)
matrix=cellvec{2}*ones(cellvec{1}(1),cellvec{1}(2));
for i=3:size(cellvec,2)
matrix(cellvec{i}(1),cellvec{i}(2))=cellvec{i}(3);
end
end
SIVA SAI AKULA on 29 Jul 2020 at 3:04
row_max = max(M,[],2);
col_min = min(M,[],1);
[row,col]=find((M == row_max).*(M == col_min));
if isempty(col) || isempty(row)
indices=[]
else
for i=1:length(row)
indices(i,:)=[row(i),col(i)];
end
end
end

Stephen Cobeldick on 15 Jun 2015
Edited: Stephen Cobeldick on 15 Jun 2015
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:
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:
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

#### 1 Comment

Ravi kumar on 2 Jul 2020

your code is not working,but thanks for the hint.

vaishak p nair on 26 Aug 2019
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 called 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 element containing the column index. If there is no saddle point in M, then indices is the empty array.
solution :
indices=[];
[a b]=size(M);
q=1;
for i=1:a
for j=1:b
x=M(i,:);
y=M(:,j);
c=M(i,j)>=x;
d=M(i,j)<=y;
if ~ismember(0,c) && ~ismember(0,d)
indices(q,1)=i;
indices(q,2)=j;
q=q+1;
end
end
end
end

#### 1 Comment

Travis Ha on 30 Jul 2020 at 23:14
I get everything except for c and d. How does it go through each row and column to find the saddle point? Can you just thoroughly explain how c and d works please?

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.

#### 1 Comment

Muhammad Usman Saleem on 14 Jun 2015
@the cyclist thanks for contributions... But i hope the refer link is not solution of my problem?? I am getting 1 1 for mat(5,3)?????

Konstantinos Sofos on 14 Jun 2015
Edited: Konstantinos Sofos on 14 Jun 2015
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.
1. Take a piece of white paper and write down a matrix 2. Try to write down an algorithm
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..
for jj =
for ii =
if ...
end
end
end
Muhammad Usman Saleem on 15 Jun 2015
Revant Shah on 24 Apr 2020
@Marco I tried this man, but somehow the code is not working. Its showing an error which says that && operator should be convertible to scalar logical value.

Jaimin Motavar on 3 Jul 2019
[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

#### 1 Comment

Faria Sultana on 30 Apr 2020
Hello, I'm learning MATLAB from the very beginning nowadays. So, I didn't understand the approach using prod function. Would you please tell me what is going on inside the built-in function 'prod'?

charu sharma on 27 Aug 2015
You should use two for loops to check for each element of a row and a column. Refer this for much simpler code: http://farzicoders.blogspot.in/2015/08/write-function-called-saddle-that-finds.html

Jos (10584) on 3 Apr 2019
[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)].'

MADDINENI REVANTH SAI on 31 Aug 2019
[r, c] = size(M);
s = [];
if r > 1
cols = min(M);
else
cols = M;
end
if c > 1
rows = max(M');
else
rows = M;
end
for ii = 1:c
for jj = 1:r
ifM(jj,ii) = cols(ii)&&M(jj)==rows(jj)
s = [s;jj ii];
end
end
end

Divya Ratna on 24 May 2020
i think anyone should try their own first rather than looking for answers in the community.
this passes all the test cases...
s = size (M);
indices = [];
for ii = 1 : s(1)
maxy = max ( M(ii,:) );
for jj = 1 : s(2)
if M(ii,jj) == maxy;
miny = min (M(:,jj));
if M(ii,jj) == miny;
indices = [indices; ii jj];
end
end
end
end
end

#### 1 Comment

Garvit Kukreja on 29 May 2020
can you help me with this.
Thankyou
[ii jj ]= size(z)
indices = [];
for i=1:ii
for j=1:jj
x(i ,j)= [ z(i,j)]
end
[p,q]= max(x(i,:)) %max value in a row. p give max value, q gives column
for k=1:ii
y(k,q)= [z(k,q)]
end
[m,n]= min(y(:,q)) %min value in a row. m give min value, n gives column
if p==m
indices = [indices; i q]
end
end
end

Garvit Kukreja on 30 May 2020
Can anyone Help me with this. Thankyou
[ii jj ]= size(z)
indices = [];
for i=1:ii
for j=1:jj
x(i ,j)= [ z(i,j)]
end
[p,q]= max(x(i,:)) %max value in a row. p give max value, q gives column
for k=1:ii
y(k,q)= [z(k,q)]
end
[m,n]= min(y(:,q)) %min value in a row. m give min value, n gives column
if p==m
indices = [indices; i q]
end
end
end

Tejas Sabu on 13 Jun 2020
Edited: Tejas Sabu on 13 Jun 2020
[m,n] = size (M);
indices=[];%we want an empty matrix if there r no saddle points
for i=1:m; %going thru all the rows and each element of the row.
maxi=max(M(i,:));% finding the max of the elements of the specific row.
for j=1:n;% running thru all the coloumns and each element of the column .
mini=min(M(:,j));% finding the min of the elements of each column.
if maxi==mini%checking if the max of a row is same as the min of a column, if yes then
indices=[indices;i j];% indices will give null matrix in first column and i and j in the next row
end
end
end
hope this helps...try to understand the code instead of copying.

Muhammad Qaisar Ali on 26 Jun 2020
another approch
indices=[];
for r=1:size((Z),1) % going through Rows
for c=1:size((Z),2) % going through Cols
if sum((Z(r,c)>=(Z(r,:))))>=size((Z),2) && sum((Z(r,c)<=(Z(:,c))))>=size((Z),1) % then saddle point
indices=[indices;[r,c]];
end
end
end
end

#### 1 Comment

Avinav Ayushman on 8 Aug 2020 at 8:31
Thanks man, it works and i got the main point also

Shiladittya Debnath on 27 Jul 2020 at 7:53
For Function :
[a,b]=size(M);
id = zeros(a+b,2);
count = 0;
for i = 1:a
mah = max(M(i,:));
[c1,c2] = find(M(i,:) == mah);
for k = 1:length(c1)
c1k = c1(k); c2k = c2(k);
mic = min(M(:,c2k));
if M(i,c2k)==mic
count = count+1;
id(count,:) = [i,c2k];
end
end
end
id = id(1:count,:);
end

Shiladittya Debnath on 27 Jul 2020 at 7:54
And for Code to Call your Function :
% create an interesting surface
[X,Y] = meshgrid(-15:0.5:10,-10:0.5:10);
Z = (X.^2-Y.^2)';
% plot surface
surf(Z);
hold on
% mark saddle points with red dots in the same figure
for ii = 1:size(indices,1)
h = scatter3(indices(ii,2),indices(ii,1),Z(indices(ii,1),indices(ii,2)),'red','filled');
h.SizeData = 120;
end
view(-115,14);
hold off

SIVA SAI AKULA on 29 Jul 2020 at 3:04
row_max = max(M,[],2);
col_min = min(M,[],1);
[row,col]=find((M == row_max).*(M == col_min));
if isempty(col) || isempty(row)
indices=[]
else
for i=1:length(row)
indices(i,:)=[row(i),col(i)];
end
end
end