MATLAB Answers

0

Indices of a maximum value in a multidimensional matrix.

Asked by Aftab Ahmed Khan on 25 Jul 2016
Latest activity Commented on by Aftab Ahmed Khan on 25 Jul 2016
Hi, I have a multidimensional matrix of size (100,16,4,10) and i want to calculate all the 4 indices of the max value in it? Any help please.

  0 Comments

Sign in to comment.

2 Answers

Answer by Azzi Abdelmalek
on 25 Jul 2016
Edited by Azzi Abdelmalek
on 25 Jul 2016
 Accepted Answer

a=rand(100,16,4,10); %Example
[max_value,idx]=max(a(:))
[x,y,z,w]=ind2sub(size(a),idx)
If the dimension of a is unknown
a=rand(100,16,4,10);
[max_value,idx]=max(a(:))
x=cell(ndims(a),1)
[x{:}]=ind2sub(size(a),idx)

  3 Comments

Hi, Thank you for this working example. So, if i'm interesting in a max value of a particular slice (in this case 25) of it then it doesn't return the correct result. The way, i am trying is like this.
a=rand(100,16,4,10);
[max_value,idx]=max(a(25,:,:,:));
[w,x,y,z]=ind2sub(size(a),idx);
use
b = squeeze(a(25,:,:,:));
and compute x, y, z from b; w is 25 by definition.
Thank you. works like a charm.

Sign in to comment.


Answer by Adam
on 25 Jul 2016
Edited by Adam
on 25 Jul 2016

[maxVal, idx] = max( myMatrix(:) );
[x, y, z, w] = ind2sub( size( myMatrix ), idx );

  0 Comments

Sign in to comment.