Index of maximum value element in each row (last index)

Viewed 651

I need to find index of maximum element in each row in matrix in MATLAB. Something like

[~,indexes] = maxValues = max(p_y_x,[],2);

works fine, but I need to get LAST index (when there is more than one with maximum value). Now I have something like this:

N=size(p_y_x,1);
maxValues = max(p_y_x,[],2);
indexes=zeros(1,N);

for n=1:N
   indexes(n)=find(p_y_x(n,:)==maxValues(n),1,'last');
end

Which is complicated and not very efficient (because of the for loop).

I doubt something that trivial must be done that way. Is there a more optimal solution?

3 Answers
Related