find max value in a Matrix using loops only

8 vues (au cours des 30 derniers jours)
Zed Ferch
Zed Ferch le 10 Nov 2019
Commenté : Zed Ferch le 10 Nov 2019
hallo,
I can find only two max with this code. can you help me please to find all Max (more than 1 or 2) with the corrent code in this Matrix X=[1,2,3;7,1,9;4,9,6;9,8,7].
X=[1,2,3;7,1,9;4,9,6;9,8,7];
Max = -Inf;
row = 0;
column = 0;
row1 = 0;
column1 = 0;
for i = 1:size(X, 1)
for j = 1:size(X, 2)
if X(i, j) > Max
Max = X(i, j);
row = i;
column = j;
end
if X(i, j) >= Max
Max = X(i, j);
row1 = i;
column1 = j;
end
end
end
X
Max
row
column
row1
column1
  1 commentaire
dpb
dpb le 10 Nov 2019
Find global maximum first, then find the matching locations. Save the location indices in arrays instead of named variables. Mimic the output of the builtin max function.

Connectez-vous pour commenter.

Réponse acceptée

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH le 10 Nov 2019
solution:
X=[1,2,3;7,1,9;4,9,6;9,8,7];
Max = -Inf;
row = [];
column = [];
for i=1:size(X,1)
for j=1:size(X,2)
if X(i,j)>=Max
Max=X(i,j);
end
end
end
for i=1:size(X,1)
for j=1:size(X,2)
if X(i,j)==Max
row(end+1)=i;
column(end+1)=j;
end
end
end
X
Max
row
column
  1 commentaire
Zed Ferch
Zed Ferch le 10 Nov 2019
Hallo Jesus David,
thank you very much for your help, the code word very well, thats nice :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by