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
|
|
|
|
Reply
|
De
|
12/9/2010 11:40:31 PM |
|
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);
|
|
0
|
|
|
|
Reply
|
Walter
|
12/9/2010 11:59:54 PM
|
|
> 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/
|
|
0
|
|
|
|
Reply
|
Ahmed
|
12/10/2010 12:01:07 AM
|
|
Thank you guys! It really helped. I understand about the concept much better now. :)
|
|
0
|
|
|
|
Reply
|
De
|
12/10/2010 7:22:04 AM
|
|
"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 (6341)
|
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
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
|
|
0
|
|
|
|
Reply
|
slord (13279)
|
4/12/2013 1:25:15 PM
|
|
|
6 Replies
775 Views
(page loaded in 0.022 seconds)
Similiar Articles: Finding the maximum value of a matrix - comp.soft-sys.matlab ...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 kno... How can I find the maximum value of a sine wave ? (Simulink ...I generate a sine wave during 2 second (simulation time) and I want to find the maximum's value of this wave during these 2 second. I tried the minmax block and maximum ... Finding n largest values in a matrix - comp.soft-sys.matlab ...Finding the maximum value of a matrix - comp.soft-sys.matlab ... Finding n largest values in a matrix - comp.soft-sys.matlab ... How to find largest value? - comp.soft-sys ... Finding First 50 max. values in an array - comp.soft-sys.matlab ...Finding the maximum value of a matrix - comp.soft-sys.matlab ... Finding First 50 max. values in an array - comp.soft-sys.matlab ... Finding the maximum value of a matrix ... EEG data maximum and minimum values - comp.soft-sys.matlab ...Finding the maximum value of a matrix - comp.soft-sys.matlab ... Standardizing values in a matrix - comp.soft-sys.matlab finding maximum value in a data set ... finding values in columns and returning the rows - comp.soft-sys ...Finding the maximum value of a matrix - comp.soft-sys.matlab ... finding values in columns and returning the rows - comp.soft-sys ... Finding the maximum value of a matrix ... Find the position of max/min value in an unsorted array - comp ...Finding the maximum value of a matrix - comp.soft-sys.matlab ..... max value of the whole matrix, and I want to find the location ... find out the Nth maximum/minimum ... Standardizing values in a matrix - comp.soft-sys.matlabFinding the maximum value of a matrix - comp.soft-sys.matlab ... Standardizing values in a matrix - comp.soft-sys.matlab finding maximum value in a data set and ... finding maximum value in a data set and subtracting from plot ...Finding the maximum value of a matrix - comp.soft-sys.matlab ... finding maximum value in a data set and subtracting from plot ... Finding the maximum value of a matrix ... How to find largest value? - comp.soft-sys.matlabFinding the maximum value of a matrix - comp.soft-sys.matlab ... How to find largest value? - comp.soft-sys.matlab How to find largest value? - comp.soft-sys.matlab how to ... Night Walker: MATLAB: find the maximum in a matrixGiven a matrix a, a = 81 91 27 96 95 90 63 54 15 48 12 9 95 97 80 now we want to locate the maximum value 97. Finding the maximum value of a matrix - Newsreader - MATLAB CentralI 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 ... 7/23/2012 6:32:45 PM
|