How to get the largest element index in matlab array

Here is a simple double array:

array=[3 1 1]

Largest element index is 1

or:

array=[3 9 1]

Largest element index is 2

How can I get the largest element index?

Answers


Use the second output argument of the max function:

[ max_value, max_index ] = max( [ 3 9 1 ] )

My standard solution is to do

index = find(array == max(array), 1);

which returns the index of the first element that is equal to the maximum value. You can fiddle with the options of find if you want the last element instead, etc.


If you need to get the max value of each row you can use:

array = [1, 2, 3; 6, 2, 1; 4, 1, 5];
[max_value max_index] = max(array, [], 2)

%3, 3
%6, 1
%5, 3

In Octave If
A =
   1   3   2
   6   5   4
   7   9   8

1) For Each Column Max value and corresponding index of them can be found by
>> [max_values,indices] =max(A,[],1)
max_values =
   7   9   8
indices =
   3   3   3


2) For Each Row Max value and corresponding index of them can be found by
>> [max_values,indices] =max(A,[],2)
max_values =
   3
   6
   9
indices =
   2
   1
   2

Similarly For minimum value

>> [min_values,indices] =min(A,[],1)
min_values =
   1   3   2

indices =
   1   1   1

>> [min_values,indices] =min(A,[],2)
min_values =
   1
   4
   7

indices =
   1
   3
   1

Need Your Help

Is there a URL query string for searching own GitHub Gists?

github github-api gist

Is there a URL query string for searching only own GitHub Gists? I wouldn't care if it only worked with public Gists.

How to remove all rows in a numpy.ndarray that contain non-numeric values

python numpy

Basically, I'm doing some data analysis. I read in a dataset as a numpy.ndarray and some of the values are missing (either by just not being there, being NaN, or by being a string written "NA").