MATLAB Answers

0

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

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

  3 Comments

function indices = saddle(M)
X = (max(M,[],2))'; % operation on row arranged in a column i.e maximum of each row arranged in a column and then transpose gives the row vector of the same
Y = min(M,[],1); %operation on column arranged in row i.e. minimun of each column arranged in a row
count=0;
for i=1:size(X,2)
for j=1:size(Y,2)
if X(i)==Y(j)
count = count+1;
indices(count,:) = [i,j];
end
end
end
if count==0
indices = double.empty;
end
I have an issue for returning an empty matrix if there are no saddle points. I dont know where to put the statement in my code. I currently have it under the check if a point is a saddle point, but whenever a point is not, it returns the empty matrix. If i delete the else statement, the code works for matrices with saddle points.
function indices = saddle(M)
row = size(M,1);
col = size(M,2);
if row==1
maximum = max(M);
[first, second]= find(M==maximum);
indices=[first;second]';
else
maximums = max(M,[],2)';
minimums = min(M,[],1);
for i=1
for j=1:col
for x=1
for y=1:col
if maximums(i,j)==minimums(x,y) %check if a point is a saddle point
[first,second]= find(M==maximums(i,j));
indices=[first second]; %returns the indices of the saddle points
else %else statement if a matrix has no saddle points
indices=[];
break
end
end
end
end
end
end
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...

Sign in to comment.

Products

6 Answers

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

  0 Comments

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.

  1 Comment

@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)?????

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,

  2 Comments

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

Sign in to comment.


Answer by 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

  0 Comments

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)].'

  0 Comments

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

  0 Comments

Sign in to comment.