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 件のコメント

Vishal
Vishal 2023 年 2 月 7 日
what will be the execution code.

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 6 日

0 投票

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 件)

Nilesh Khodiar
Nilesh Khodiar 2021 年 7 月 3 日
編集済み: Nilesh Khodiar 2021 年 7 月 3 日

0 投票

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 ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by