MATLAB Answers

Regarding a function saddle

40 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 6 Jun 2020
In first code, 'j' varies from 1 to b
for j=1:b
In second code, 'j' varies from i to b
for j = i:b

  0 Comments

Sign in to comment.

More Answers (0)

Tags