help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: index of max element of a 4-D matrix


From: Jaroslav Hajek
Subject: Re: index of max element of a 4-D matrix
Date: Mon, 12 Apr 2010 10:03:46 +0200

On Mon, Apr 12, 2010 at 9:28 AM, Tim Rueth <address@hidden> wrote:
> I am currently using a for-loop to parse through a 4-D matrix to find the
> index of the maximum-value element.  I'm sure this can be done much simpler
> using commands like max() and find().  Conceptually, I'd like to just say:
>
> max_idx = find(max(matrix_4d))
>
> but I know this doesn't work because of how max() finds the max of each
> column, and returns a matrix itself.  So, maybe something like:
>
> [max_matrix idx_matrix] = max(max(max(max(matrix_4d))))
>
> This gives me the correct max value, but not the intended index, again, due
> to how the index is determined from each resulting max matrix.
>
> Does anyone know a vectorized way of determining the index of the
> maximum-value element in a 4-D matrix that's faster than a for loop?
>
> Thanks.
>

[max_val, max_idx] = max (matrix_4d(:));

gives you a linearized index. To get the 4-tuple of subscripts, use
[i1, i2, i3, i4] = ind2sub (size (matrix_4d), max_idx);

hth

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]