# write a function called saddle in the input matrix M , the function should return with exactly two column vector and each row of output vector represent dimensions of saddle point in the input matrix M

90 views (last 30 days)
Soham Bhosale on 10 Jun 2020
Edited: DGM on 28 Aug 2023 at 16:15
indice=[];
r=0;
for n1=1:size(M,1)
maximum=max(M(n1,:));
point=find(M(n1,:)==maximum);
lowest=min(M(:,point));
if maximum == lowest
condition=1;
else
condition=0;
end
if condition==1
r=r+1;
indice(r,1)=n1;
indice(r,2)=point;
end
indices=indice;
end
Rik on 9 Oct 2020
I would suggest removing the code and leaving the code comments. That way students can fill in a framework solution and learn from you.

Duong Nguyen on 14 Mar 2022
Hi, I got my answer for this question as shown below. I am wondering that if there is any test cases fail with my answer? Thank you!
indices = [];
[row,~] = size(M);
for i = 1:row
Max = strfind(M(i,:),max(M(i,:))); [~, indMax] = size(Max);
for ii = 1:indMax
if M(i,Max(ii)) == min(M(:,Max(ii)))
indices = [indices; [i, Max(ii)]];
end
end
end
end
##### 2 CommentsShow 1 older commentHide 1 older comment
If i'm right, you are using strfind to compare each element of the matrix in the for loop so you can evaluate if it's greater or equal to...right?
On the other hand, can you explain the line: indices = [indices; [i, Max(ii)]];
Why do you write indices inside the argument?

Chandan Kumar on 18 Mar 2021
Edited: DGM on 28 Aug 2023 at 16:15
[r, c] = size(M);
% Initialize the saddle points to an empty array
% Check the dimensions to see if input is a row or column vector
% find the min value in each column if more than 1 row
% min would give a single value
% find the max value in each row
% max would give a single value
% visit each column
% and each row, that is, each element of M
if M(jj,ii) == cols(ii) && M(jj,ii) == rows(jj) % if both conditions hold
end
Rik on 18 Mar 2021
Also, why did you answer this question with a complete solution? That only encourages cheating.

Ankit Sharma on 24 Jun 2022
% I have write this code in matlab 2015
% creating a function named as saddle
function indices = saddle(M) % input matrix is M
s = size(M); % finding the size of the M matrix
[max_per_row] = max(M , [] ,2); % finding the maximum in row by max command, here 2 reads the row
[min_per_col] = min(M , [] ,1); % finding the minimum in column by min command, here 1 reads the column
indices = []; % assining th =e indices to empty matrix if there is no saddle point present then it returns empty matrix there
% creating a loop to check the requireties like, whose value is
% greater then or equal to every element in row, and less then or
% equal to every element in column
for j=1:s(2)
for i=1:s(1)
if M(i,j) >= max_per_row(i) && M(i,j) <= min_per_col(j) % here we are applying the condition to check requireties
indices = [indices; [i, j]] % if any indices find then assigning the value of indices
end
end
end
end

Vishnu V on 22 Oct 2022
[m,n]=size(M);
indices = [];
for ii = 1 : m %accessing each elements of M
for jj = 1 : n
if sum(M(ii,jj)>=M(ii,:))==n % checking whether the element is greater than or equal to all the other elements in the row
if sum([M(ii,jj)<=M(:,jj)]')==m % checking whether the element is lesserr than or equal to all the other elements in the column
indices = [ indices; ii jj]; % append the indices of saddle element
end
end
end
end

David on 20 Jan 2023
[r, c] = size(M);
% Initialize the saddle points to an empty array
s = [];
% Check the dimensions to see if input is a row or column vector
if r > 1
cols = min(M); % find the min value in each column if more than 1 row
else
cols = M; % vector is a special case, min would give a single value
end
if c > 1
rows = max(M'); % find the max value in each row
else
rows = M; % vector is a special case, max would give a single value
end
for ii = 1:c % visit each column
for jj = 1:r % and each row, that is, each element of M
if M(jj,ii) == cols(ii) && M(jj,ii) == rows(jj) % if both conditions hold
end
end
end
##### 2 CommentsShow 1 older commentHide 1 older comment
DGM on 6 Feb 2023
Oh that's easy:

youssef on 28 Aug 2023 at 11:38
indices=[];
[a,b]=size(M);
rows=cell(a,1);
%making the ceLLs
for i=1:a
rows{i,1}=M(i,:);
end
%finding max of each row
maxindx=[];
for i=1:a
tempcellrow=rows{i,1};
c1=find(tempcellrow==max(tempcellrow));
for q=1:length(c1)
maxindx=[maxindx;i c1(q)];
end
end
%maxindex now holds the indices of all elements which are the max of its
%row and with respect to the repetition if exist
%Lets check these element by its indecies if they are the min of their cols
count=0;
[L,~]=size(maxindx);
for c=1:L
check=M(maxindx(c,1),maxindx(c,2));
for i=1:a
if check<=M(i,maxindx(c,2))
count=count+1;
end
end
if count==a
indices=[indices;maxindx(c,:)];
end
count=0;
end
end

### Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!