Find the last position of maximum value in a Matrix

38 次查看(过去 30 天)
Hello, I try to find the position and the value of a maximum in a Matrix (or in a vektor). There are several positions for the maximas and the function [C,I] = max(...) just return the first, but I need the last position. exists a method to find the last maximum?

采纳的回答

Wayne King
Wayne King 2013-11-27
编辑:Wayne King 2013-11-27
You can use find() with the 'last' argument:
x = randi([1 10],100,1);
maxval = max(x);
I = find(x==maxval,1,'last');
Or for a matrix:
X = randi([1 10],20,20);
maxval = max(X(:));
[I,J] = find(X==maxval,1,'last');

更多回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-11-27
x=[1 2 3 0 3];
[ii,jj]=max(fliplr(x));
idx=numel(x)-jj+1

Bradley Stiritz
Bradley Stiritz 2021-1-31
@Wayne, is your solution vulnerable to rounding error, with floating-point input? In general, might it not be safer to use something like the following--?
epsilon = 0.0001;
I = find(abs(x-maxval)<epsilon,1,'last');
@Azzi, very clever solution but is it not hard-coded for row vectors? In order to handle column vectors as well, shouldn't the 2nd line be something like the following--?
[ii,jj]=max(fliplr(x(:)'));

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by