user3089676
user3089676

Reputation: 81

How to read the indices of my max value in any given array using a created function

I have created a function that should be able to read in any mxn matrix and gives me the maximum value of the entire matrix (not just per column) and what its indices are.

function [ BIGGEST ] = singlemax( x )


[Largest_values row]=max(x)

[biggest_number column] = max(max(x))

end

This function gives me all the information I need, however it is not very clean as it gets messy the larger the matrix.

The real problem area is printing out the row in which the maxima is located.

Largest_values =

    0.7750    0.9122    0.7672    0.9500    0.6871


row =

     3     2     3     2     2


biggest_number =

    0.9500


column =

     4

This is my print out given a random matrix as an input.With the function I have created I cannot read the indices of my max value in any given array using a created function. If I could somehow relate the maximas from each column and there corresponding row (such as making the results a matrix with the column max on top and the row index on bottom, all within the same respective columns )I could display the row of the absolute maximum.

Upvotes: 1

Views: 71

Answers (1)

David
David

Reputation: 8459

Here's one approach:

value = max(x(:));
[rowIndex,columnIndex] = ind2sub(size(x),find(x==value));

Read the ind2sub documentation for more details.

Edited to modify so that it finds indices of all occurrences of the maximum value.

Upvotes: 2

Related Questions