[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: index of max element of a 4-D matrix
From: |
Andy Buckle |
Subject: |
Re: index of max element of a 4-D matrix |
Date: |
Mon, 12 Apr 2010 08:57:07 +0100 |
I am not sure if this is the most elegant way, but it seems to work.
a=zeros(4,4,4,4);
a(1,2,3,4)=2;
[m idx4]=max(max(max(max(a(: ,: ,: ,: )))))
[m idx3]= max(max(max(a(: ,: ,: ,idx4))))
[m idx2]= max(max(a(: ,: ,idx3,idx4)))
[m idx1]= max(max(a(: ,idx2,idx3,idx4)))
max_val=a(idx1,idx2,idx3,idx4)
Andy
On Mon, Apr 12, 2010 at 8: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.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>
>
--
/* andy buckle */