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

 采纳的回答

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

更多回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by