how to find maximum value in a matrix?
Afficher commentaires plus anciens
Now, there is reason why I don't want to use a max() or similar built-in function to find the maximum value of a matrix. My question is following:
Imagine, we have a matrix, let's call it 'gauss' and looks like this:
gauss =
0 0.2000 0
0.5000 1.0000 0.6000
0 0.3000 0
What I want is to find the maximum, i.e. 1 or the value at gauss(2,2). I need to automize a step search from gauss(1,1) so that I end up finding gauss(2,2) in this entire matrix.
Would be glad if you could give some idea regarding the coding of the steps.
Thanks!
2 commentaires
Anton Semechko
le 31 Mai 2018
It is in general not possible to find (global) maximum value of the matrix when performing local steps, from one matrix element to its adjacent neighbours. If you do so, then for any given starting position, the algorithm will converge onto a local maximum, which may or may not be a global maximum.
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 1 Juin 2018
gr = size(gauss,1);
gc = size(gauss,2);
rowpos = 1; colpos = 1;
improved = true;
while improved
bestval = gauss(rowpos, colpos);
improved = false;
for rowtry = max(1, rowpos-1) : min(rowpos+1, gr)
for coltry = max(1, colpos-1) : min(colpos+1, gc)
newval = gauss(rowtry, coltry);
if newval > bestval
bestval = newval;
rowpos = rowtry;
colpos = coltry;
improved = true;
break;
end
end
if improved; break; end
end
end
If it did not get caught in a local minima, then afterwards rowpos and colpos will be the location of the peak, and bestval will be the value there.
Catégories
En savoir plus sur Geometric Transformation and Image Registration dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

