Be the first to rate this file! 2 Downloads (last 30 days) File Size: 397 Bytes File ID: #18518

indmax

by Andreas Hoechner

 

29 Jan 2008 (Updated 30 Jan 2008)

Finds maximum and its index of an N-dimensional array.

| Watch this File

File Information
Description

[ind,maxi]=indmax(x)

Finds index to and maximum of N-dimensional array

ind is cell array
maxi is scalar

Example:
r=rand([2 3 4])
[ind,maxi]=indmax(r)
so that
maximum=r(ind{:})

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
30 Jan 2008 w s

Some comments and questions:

indmax finds the subscripts and not single index. So the function should rather be termed submax.

indmax returns only the first occurrence of max(A). Wouldn't it be better that all indices or subscripts are returned where A==max(A)?

indmax returns the indices (subscripts) as cell array. Wouldn't it be better to return them as row vector? Because this here (taken from your example) returns an error.

A = cat(3,magic(4),flipud(magic(4)+1));
[ind,maxi]=indmax(A)

ind =

    [4] [1] [2]

maxi =

    17

maximum = A(ind)

??? Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'cell'.

30 Jan 2008 Duane Hanselman

For an alternative see the functions minn and maxn, file id # 4719, on the File Exchange

06 Feb 2008 Andreas Hoechner

dear w s

'So the function should rather be termed submax.'
-yes

'Wouldn't it be better that all indices or subscripts are returned where A==max(A)?' -sometimes, in my case not...

Wouldn't it be better to return them as row vector?'
- not necessarily. and the syntax (see example) is not maximum=A(ind) but maximum=A(ind{:})

anyway, i thought the recursive loop was quite elegant, but of course, using the linear index is faster and easier. i was not aware of maxn by douane hanselman and recommend to use his routine.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
max Andreas Hoechner 22 Oct 2008 09:45:27
indmax Andreas Hoechner 22 Oct 2008 09:45:27
cell array Andreas Hoechner 22 Oct 2008 09:45:27
ndimensional Andreas Hoechner 22 Oct 2008 09:45:27
index Andreas Hoechner 22 Oct 2008 09:45:27

Contact us at files@mathworks.com