Regarding a function saddle
显示 更早的评论
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 个)
Nilesh Khodiar
2021-7-3
编辑:Nilesh Khodiar
2021-7-3
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!