1.0

1.0 | 4 ratings Rate this file 7 Downloads (last 30 days) File Size: 478 Bytes File ID: #19572

maxmax

by Simon

 

14 Apr 2008 (Updated 14 Apr 2008)

find the max value and position from a multi-dimensional matrix

| Watch this File

File Information
Description

The maxmax function can find the max value and return the positions from a multi-dimensional matrix.
[max_value max_id]=maxmax(x)
The max_value is the biggest value in matrix x, and max_id is structured in [dim1 dim2 dim3...], the length of max_id is equal to the number of the dimensions of matrix x.
This method can be also applied to min and other functions.
e.g.

x=rand(2,8,4);
[max_value max_id]=maxmax(x)

MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
14 Apr 2008 Jos x@y.z

This submission has no help, contains no comments at all, and is poorly coded.

m = max(x(:)) ;
linind = find(x==m) ;

14 Apr 2008 Urs (us) Schwarz

no help -and- even worse, does not work if the input has more than one max value...
eg,

x=ceil(3*rand(2,3,4));
% one of the many solutions...
mv=max(x(:));
mx=find(x(:)==mv);
ix=cell(1,ndims(x));
[ix{:}]=ind2sub(size(x),mx);
ix=[ix{:}];
disp(mv); % max value
disp(ix); % indices

us

14 Apr 2008 fex observer

If these two well-known reviewers tell the truth, you should remove this submission.

31 Jan 2009 Steeve AMBROISE

Another way, if you want to use the eval function and manipulate strings:

NDim = ndims(X); maxim = 'X'; Indices = ' ';
for i= 1:NDim,
    maxim = strcat('max(',maxim,')');
    Indices = strcat(Indices, 'I', num2str(i),', ');
end
Max = eval(maxim);
Indices = strcat('[', Indices(1:end-1), ']');
eval([Indices, '= ind2sub(size(X), find(X==Max));']);
Ind = eval(Indices);

It works even if several elements have the largest value.

You can use the preceding example.

S.A.

01 Feb 2009 Jos (10584)

The solution suggested by S.A. is (almost) as poor as the original submission ...

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
max max Simon 22 Oct 2008 09:57:49
maxmax Simon 22 Oct 2008 09:57:49
multidimensional Simon 22 Oct 2008 09:57:49
matrix Simon 22 Oct 2008 09:57:49

Contact us at files@mathworks.com