Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to know the location of max. value of a matrix

Subject: How to know the location of max. value of a matrix

From: Yi

Date: 4 Jan, 2010 22:01:20

Message: 1 of 7

Hey my Matlab friends:

I have a huge maxtrix (5000*5000), but for simplicity, I will use a 4*4 matrix to ask questions,

suppose I have a matrix A

A = [1 2 3 4;
       2 3 4 5;
       1 8 3 7;
       3 3 4 7]

The maximum of value in this matrix is obviously 8, in row 3 and column 2. My question is how to ask matlab return me a value that can tell me the location of this max value of the matrix.

If the matrix have two or more maximum value, what the computer will do, give me the first max. value that Matlab scans?

Thanks and Happy New Year

Yi

Subject: How to know the location of max. value of a matrix

From: Nathan

Date: 4 Jan, 2010 22:11:18

Message: 2 of 7

On Jan 4, 2:01 pm, "Yi " <hva...@163.com> wrote:
> Hey my Matlab friends:
>
> I have a huge maxtrix (5000*5000), but for simplicity, I will use a 4*4 matrix to ask questions,
>
> suppose I have a matrix A
>
> A = [1 2 3 4;
>        2 3 4 5;
>        1 8 3 7;
>        3 3 4 7]
>
> The maximum of value in this matrix is obviously 8, in row 3 and column 2. My question is how to ask matlab return me a value that can tell me the location of this max value of the matrix.
>
> If the matrix have two or more maximum value, what the computer will do, give me the first max. value that Matlab scans?
>
> Thanks and Happy New Year
>
> Yi

help max
help ind2sub

[num idx] = max(A(:));
[x y] = ind2sub(size(A),idx);
%%%%%%%%%%%%
x =
     3
y =
     2


Test for multiple max values:
A(4,3:4) = 8
%%%%%%%%%%%%%
A =
     1 2 3 4
     2 3 4 5
     1 8 3 7
     3 3 8 8
%%%%%%%%%%%%%
[num] = max(A(:));
[x y] = ind2sub(size(A),find(A==num))
%%%%%%%%%%%%%
x =
     3
     4
     4
y =
     2
     3
     4

Each ordered pair (x,y) contains the max value (8).

-Nathan

Subject: How to know the location of max. value of a matrix

From: Yi

Date: 5 Jan, 2010 04:05:04

Message: 3 of 7

Thank you Nathan, your answer is really great. It is very helpful. Thanks

Yi

Nathan <ngreco32@gmail.com> wrote in message <d9209b50-e864-44cd-962f-b0a414341949@a6g2000yqm.googlegroups.com>...
> On Jan 4, 2:01 pm, "Yi " <hva...@163.com> wrote:
> > Hey my Matlab friends:
> >
> > I have a huge maxtrix (5000*5000), but for simplicity, I will use a 4*4 matrix to ask questions,
> >
> > suppose I have a matrix A
> >
> > A = [1 2 3 4;
> >        2 3 4 5;
> >        1 8 3 7;
> >        3 3 4 7]
> >
> > The maximum of value in this matrix is obviously 8, in row 3 and column 2. My question is how to ask matlab return me a value that can tell me the location of this max value of the matrix.
> >
> > If the matrix have two or more maximum value, what the computer will do, give me the first max. value that Matlab scans?
> >
> > Thanks and Happy New Year
> >
> > Yi
>
> help max
> help ind2sub
>
> [num idx] = max(A(:));
> [x y] = ind2sub(size(A),idx);
> %%%%%%%%%%%%
> x =
> 3
> y =
> 2
>
>
> Test for multiple max values:
> A(4,3:4) = 8
> %%%%%%%%%%%%%
> A =
> 1 2 3 4
> 2 3 4 5
> 1 8 3 7
> 3 3 8 8
> %%%%%%%%%%%%%
> [num] = max(A(:));
> [x y] = ind2sub(size(A),find(A==num))
> %%%%%%%%%%%%%
> x =
> 3
> 4
> 4
> y =
> 2
> 3
> 4
>
> Each ordered pair (x,y) contains the max value (8).
>
> -Nathan

Subject: How to know the location of max. value of a matrix

From: Vagelis Bebelis

Date: 7 Feb, 2011 16:08:03

Message: 4 of 7

"Yi " <hvachy@163.com> wrote in message <hhtofg$pm1$1@fred.mathworks.com>...
> Hey my Matlab friends:
>
> I have a huge maxtrix (5000*5000), but for simplicity, I will use a 4*4 matrix to ask questions,
>
> suppose I have a matrix A
>
> A = [1 2 3 4;
> 2 3 4 5;
> 1 8 3 7;
> 3 3 4 7]
>
> The maximum of value in this matrix is obviously 8, in row 3 and column 2. My question is how to ask matlab return me a value that can tell me the location of this max value of the matrix.
>
> If the matrix have two or more maximum value, what the computer will do, give me the first max. value that Matlab scans?
>
> Thanks and Happy New Year
>
> Yi

A simple solution could be:
max_val = max(A);
index = find(A = max_val);

which returns all the positions that the matrix has the max value.

Subject: How to know the location of max. value of a matrix

From: Matt Fig

Date: 7 Feb, 2011 16:18:05

Message: 5 of 7

"Vagelis Bebelis" wrote in message <iip5d3$lut$1@fred.mathworks.com>...
> A simple solution could be:
> max_val = max(A);
> index = find(A = max_val);
>
> which returns all the positions that the matrix has the max value.

This solution will not work. First, max_val will be a row vector containing the maximum value from each column in A. Second, the = in the call to FIND will cause an error. Even if this is fixed to read ==, then you are comparing an M-by-N matrix to an 1-by-N vector, and the call will error.

Subject: How to know the location of max. value of a matrix

From: Vagelis Bebelis

Date: 7 Feb, 2011 16:25:04

Message: 6 of 7

"Matt Fig" wrote in message <iip5vt$m5$1@fred.mathworks.com>...
> "Vagelis Bebelis" wrote in message <iip5d3$lut$1@fred.mathworks.com>...
> > A simple solution could be:
> > max_val = max(A);
> > index = find(A = max_val);
> >
> > which returns all the positions that the matrix has the max value.
>
> This solution will not work. First, max_val will be a row vector containing the maximum value from each column in A. Second, the = in the call to FIND will cause an error. Even if this is fixed to read ==, then you are comparing an M-by-N matrix to an 1-by-N vector, and the call will error.

Indeed the == is an error. For multiple dimension you can try max(max()) until you get a single value (not a vector).

Subject: How to know the location of max. value of a matrix

From: Sean de

Date: 7 Feb, 2011 16:46:04

Message: 7 of 7

"Vagelis Bebelis" wrote in message <iip6d0$qco$1@fred.mathworks.com>...
> "Matt Fig" wrote in message <iip5vt$m5$1@fred.mathworks.com>...
> > "Vagelis Bebelis" wrote in message <iip5d3$lut$1@fred.mathworks.com>...
> > > A simple solution could be:
> > > max_val = max(A);
> > > index = find(A = max_val);
> > >
> > > which returns all the positions that the matrix has the max value.
> >
> > This solution will not work. First, max_val will be a row vector containing the maximum value from each column in A. Second, the = in the call to FIND will cause an error. Even if this is fixed to read ==, then you are comparing an M-by-N matrix to an 1-by-N vector, and the call will error.
>
> Indeed the == is an error. For multiple dimension you can try max(max()) until you get a single value (not a vector).

That is a very bad method of doing it that will return meaningless results for index.
The better method:
max(x(:))

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us