How can I store values that satisfies only the specific condition from the loop without overwrriting?

1 view (last 30 days)
Basically, I am trying to write code for this problem.
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,the indices is an empty array.
I tried solving it in my way,
function indices=Saddle(M)
[a,b]=size(M);
P=max(M,[],2);
Q=min(M);
for i=1:a
for j =1:b
if P(i,1)==Q(1,j)
F(i)=i; F1(j)=j;
end
end
end
if F==0
indices=[];
else
indices=[F;F1];
end
When i use the loop, I only get one value for row and column. And all above that were over written.
Is there any way I can get all the values in F(i) and F1(j) for which the condition P(i,1) and Q(1,j) is satisfied.
One thing I would like to add here is;
In order to solve this I took an example where i defined M as
M=[1,2,3,4;2,3,5,5;1,2,1,2]

Answers (1)

Sudhakar Shinde
Sudhakar Shinde on 25 Sep 2020
function indices=Saddle(M)
[a,b]=size(M);
P=max(M,[],2);
Q=min(M);
indices = {};
for i=1:a
for j =1:b
if P(i,1)==Q(1,j)%my condition which needs to be satisfied
indices{end+1} = [i,j];
end
end
end

Community Treasure Hunt

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

Start Hunting!