Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Quick way
Replies: 1   Last Post: Jul 29, 2013 10:36 AM

 Messages: [ Previous | Next ]
 Steven Lord Posts: 18,038 Registered: 12/7/04
Re: Quick way
Posted: Jul 29, 2013 10:36 AM

"Christopher " <chrisholmesparker@gmail.com> wrote in message
news:ksvcd6\$7ns\$1@newscl01ah.mathworks.com...
> You can use logical operators with the find command. For example, if we
> have a matrix called matrix, as follows:
>
> matrix = [1 6 8;...
> 3 -5 2;...
> 2 2 10];
>
> and we want to find the indices corresponding to the maximum element in
> this matrix, we can simply use a condition like the following:
>
> [ i_index j_index ] = find(matrix >= max(max(matrix))); % This is a
> one-liner.
>
> In the above case, this will yield the matrix index pair [ i_index
> j_index] = [3,3], corresponding to the maximum

Or, instead of calling MAX twice and FIND once, just call MAX once.

[maximumValue, linearIndex] = max(matrix(:));

If you need row and column indices, use IND2SUB.

--
Steve Lord
slord@mathworks.com