Regarding a function saddle
Show older comments
hello,
can anyone please tell me how are these two codes different.
This code which I took from other discussion, is giving me correct answer.
function in=saddle(M)
[a,b]=size(M); %%SIZE CALCULATED
in=[]; %%'in' IS INITIALIZED AS AN EMPTY MATRIX
for i=1:a
for j=1:b
if (M(i,j)==max(M(i,:))&& M(i,j)==min(M(:,j)))
in=[in; i,j]; %%INDICES CALCULATION AND STORING TO 'in'
end
end
end
indices=in; %%FINAL INDICES AS OUTPUT ARGUMENT
end
But the below one is giving incorrect answer.
function out = saddle(m)
[a,b] = size(m);
out = [];
for i = 1:a
for j = i:b
if (m(i,j)==max(m(i,:))) && (m(i,j)==min(m(:,j)))
out = [out; i,j;];
end
end
end
end
1 Comment
Vishal
on 7 Feb 2023
what will be the execution code.
Accepted Answer
More Answers (1)
Nilesh Khodiar
on 3 Jul 2021
Edited: Nilesh Khodiar
on 3 Jul 2021
function s = saddle(M)
[r, c] = size(M);
s = [];
if r > 1
cols = min(M);
else
cols = M;
end
if c > 1
rows = max(M');
else
rows = M;
end
for ii = 1:c
for jj = 1:r
if M(jj,ii) == cols(ii) && M(jj,ii) == rows(jj)
s = [s; jj ii];
end
end
end
Categories
Find more on Creating and Concatenating 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!