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

42 views (last 30 days)
i have written this code , but its giving me error please help to correct my code ,Thanks in advance
function indices = saddle(M)
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
  3 Comments
Amrita Pritam
Amrita Pritam on 9 Oct 2020
function s = saddle(M)
% Create logical vector that are true for each saddle condition separately
minLocs = M <= min(M, [], 1);
maxLocs = M >= max(M, [], 2);
% Find the indices where both conditions are true!
[row, col] = find(minLocs & maxLocs);
% If the input is a row vector, row and col returned from the find
% function need to be transposed to fit the output format
if isrow(M)
s = [row', col'];
else
s = [row, col];
end
end
Rik
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.

Sign in to comment.

Answers (6)

Duong Nguyen
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!
function indices = saddle(M)
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 Comments
Rik
Rik on 14 Mar 2022
You don't have any comments. How is anyone supposed to learn from your answer?
Also, mlint is warning you that your indices array is growing every iteration.
To answer your question: where is 4,4 in the output?
M= [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];
indices = saddle(M)
indices = 1×2
2 2
Luis Eduardo Pacheco González
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?

Sign in to comment.


Chandan Kumar
Chandan Kumar on 18 Mar 2021
Edited: DGM on 28 Aug 2023
function s = saddle(M)
[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
s = [s; jj ii]; % saddle point! Let's add it!
end
  1 Comment
Rik
Rik on 18 Mar 2021
Please use the tools explained on this page to format your post.
Also, why did you answer this question with a complete solution? That only encourages cheating.

Sign in to comment.


Ankit Sharma
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
Vishnu V on 22 Oct 2022
function indices = saddle(M)
[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
David on 20 Jan 2023
function s = saddle(M)
[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
s = [s; jj ii]; % saddle point! Let's add it!
end
end
end

youssef
youssef on 28 Aug 2023
function indices=saddle(M)
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

Community Treasure Hunt

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

Start Hunting!