# Check for identical maximum values in an array

8 visualizzazioni (ultimi 30 giorni)
Jane Ak il 6 Set 2017
Risposto: Jane Ak il 6 Set 2017
Hi guys, I have a matrix, A = [2 4 1;8 3 2;8 8 6;4 5 3] As seen, the above matrix has more than one maximum values. How do i write an if statement testing for multiple maximum values? That is: if (code) % if more than one maximum value exists; ...; % do ... end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Walter Roberson il 6 Set 2017
"if more than one maximum value exists in the array pick the first max value and display it's index"
idx = find( A(:) == max(A(:)) );
if length(idx) > 1
disp(idx(1))
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (3)

Star Strider il 6 Set 2017
Probably the easiest way is to do logical indexing and count the number of non-zero elements to get the number of maximum values:
A = [2 4 1;8 3 2;8 8 6;4 5 3];
nr_maxvals = nnz(A == max(A(:)));
nr_maxvals =
3
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Walter Roberson il 6 Set 2017
if nnz(A == max(A(:))) > 1
...
end
Star Strider il 6 Set 2017
@Walter — Thank you.

Accedi per commentare.

José-Luis il 6 Set 2017
Modificato: José-Luis il 6 Set 2017
Number of maximum values:
numMax=sum(A(:) == max(A(:)));
if numMax > 1
end
##### 2 CommentiMostra NessunoNascondi Nessuno
Jane Ak il 6 Set 2017
Modificato: Jane Ak il 6 Set 2017
Thank you Jose. Pardon my asking, why is the sum there? Also my if statement is stated thus:
if %more than one maximum value exists in the array
%pick the first max value and display it's index
end
I'm thinking of how to apply this to your answer above. Thank you again for your time
Stephen23 il 6 Set 2017
@Jane Ak: Star Strider already explained that in their answer from three hours ago.

Accedi per commentare.

Jane Ak il 6 Set 2017
Thanks alot guys. I thought I could accept more than 1 answer. you guys are the real MVPs.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by