COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Finding the maximum value of a matrix

• Email
• Follow

```I just started to use MATLAB recently. I am trying to find maximum value of a matrix, and the location.

For example,

A= [1,2;3,4;5,6;7,8]
I know max(A) will give me the max value of each column but it is not the max value of the whole matrix, and I want to find the location of that value also. Is there a command to do that?
```
 0

See related articles to this posting

```On 09/12/10 5:40 PM, De Jong wrote:
> I just started to use MATLAB recently. I am trying to find maximum value
> of a matrix, and the location.
>
> For example,
>
> A= [1,2;3,4;5,6;7,8]
> I know max(A) will give me the max value of each column but it is not
> the max value of the whole matrix, and I want to find the location of
> that value also. Is there a command to do that?

[value, location] = max(A(:));

If you want the location in terms of rows and columns, then

[R,C] = ind2sub(size(A),location);

```
 1

```> A= [1,2;3,4;5,6;7,8]
> I know max(A) will give me the max value of each column but it is not the max value of the whole matrix, and I want to find the location of that value also. Is there a command to do that?

Welcome to Matlab. Try the following.

[maxval maxloc] = max(A(:));

1) A(:) converts a multidimensional array (in this case, a 2D matrix) into a vertical vector.
2) maxval contains the maximum value. You could also get maxval by "max(max(A))".
3) maxloc is the linear index [1] corresponding to the maximum value in A. I.e., "A(maxloc)" will return maxval. To see which row/column this corresponds to, do the following:

[maxloc_row maxloc_col] = ind2sub(size(A), maxloc);

Then, "A(maxloc_row, maxloc_col)" does exactly the same thing as "A(maxloc)" and will return maxval.

Read "help max" and "help ind2sub" carefully to understand what's going on (and read the help for every new Matlab function you come across). Enjoy.

[1] http://blogs.mathworks.com/steve/2008/02/08/linear-indexing/
```
 1

```Thank you guys! It really helped. I understand about the concept much better now. :)
```
 0

```"Ahmed Fasih" <fasih.1NOSPAM@osu.edu.nospam> wrote in message <idrqk3\$iea\$1@fred.mathworks.com>...

> [maxloc_row maxloc_col] = ind2sub(size(A), maxloc);

Is there a better way of doing this? What if I the size of A is variable? Perhaps it is 3 or 10 dimensions. How can that be coded generically?

Thanks

Chet
```
 0
Reply chet1708 (1) 4/12/2013 1:57:09 AM

```"Chester " <chet@mailinator.com> wrote in message <kk7pll\$dr7\$1@newscl01ah.mathworks.com>...
> "Ahmed Fasih" <fasih.1NOSPAM@osu.edu.nospam> wrote in message <idrqk3\$iea\$1@fred.mathworks.com>...
>
> > [maxloc_row maxloc_col] = ind2sub(size(A), maxloc);
>
> Is there a better way of doing this? What if I the size of A is variable? Perhaps it is 3 or 10 dimensions. How can that be coded generically?
>

A=rand(2,3,4,5);

[maxA loc] = max(A(:));
i = cell(1,ndims(A));
[i{:}] = ind2sub(size(A),loc);
i = [i{:}]

i =

1     1     1     5

% Bruno
```
 0
Reply b.luong5955 (6403) 4/12/2013 5:21:08 AM

```
"Chester " <chet@mailinator.com> wrote in message
news:kk7pll\$dr7\$1@newscl01ah.mathworks.com...
> "Ahmed Fasih" <fasih.1NOSPAM@osu.edu.nospam> wrote in message
> <idrqk3\$iea\$1@fred.mathworks.com>...
>
>> [maxloc_row maxloc_col] = ind2sub(size(A), maxloc);
>
> Is there a better way of doing this? What if I the size of A is variable?
> Perhaps it is 3 or 10 dimensions. How can that be coded generically?

Ideally, by avoiding converting the location from a linear index into
subscripts.

If you're interested in finding the element in another matrix that
corresponds to the maximum element in A, you can use the linear index to do
that.

Alternately, if you must have the subscripts:

C = cell(1, ndims(A));
[C{:}] = ind2sub(size(A), maxloc);
B = 1-A;
B(C{:})

--
Steve Lord
slord@mathworks.com
http://www.mathworks.com

```
 0
Reply slord (13686) 4/12/2013 1:25:15 PM

6 Replies
1057 Views

Similar Articles

12/7/2013 2:34:44 AM
page loaded in 72303 ms. (0)