# 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

31 visualizzazioni (ultimi 30 giorni)
Soham Bhosale il 10 Giu 2020
Modificato: DGM il 28 Ago 2023
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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Amrita Pritam il 9 Ott 2020
% 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 il 9 Ott 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.

Accedi per commentare.

### Risposte (6)

Duong Nguyen il 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 CommentiMostra NessunoNascondi Nessuno
Rik il 14 Mar 2022
Also, mlint is warning you that your indices array is growing every iteration.
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 = 1×2
2 2
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?

Accedi per commentare.

Chandan Kumar il 18 Mar 2021
Modificato: DGM il 28 Ago 2023
[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
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Rik il 18 Mar 2021
Also, why did you answer this question with a complete solution? That only encourages cheating.

Accedi per commentare.

Ankit Sharma il 24 Giu 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Vishnu V il 22 Ott 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

David il 20 Gen 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 CommentiMostra NessunoNascondi Nessuno
Vishal il 6 Feb 2023
what is the code to genreate the following function
DGM il 6 Feb 2023
Oh that's easy:

Accedi per commentare.

youssef il 28 Ago 2023
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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by