0
votes

How can I get a maximum of a three-dimensional matrix (which was previously two-dimensional and converted to a three-dimensional matrix by reshape) in MATLAB so that I can then get the position of that maximum value in the matrix?

I wrote the following code, but unfortunately the dimensions obtained for the maximum values are larger than the dimensions of the matrix.

mxshirin=max(max(frvrdin))

[X,Y,Z]=size(frvrdin)
[o,i]=find(frvrdin==mxshirin)
xo=size(o)
xi=size(i)
1

1 Answers

0
votes

If frvrdin is 3D, max(max(frvrdin)) will be a 1x1x3 vector:

frvrdin = rand(3,3,3);
max(max(frvrdin))
ans(:,:,1) =
    0.8235
ans(:,:,2) =
    0.9502
ans(:,:,3) =
    0.7547

Don't nest max() functions, simply use the 'all' switch to take the max of the entire matrix at once.

max(frvrdin,[],'all')
ans =
    0.9340

If you're on an older MATLAB, use column flattening: max(frvrdin(:)).

You can't use the automated location output of max [val,idx]=max() on more than two dimensions, so use find and ind2sub:

frvrdin = rand(3,3,3);
val = max(frvrdin,[],'all');  % Find maximum over all dims
idx = find(abs(frvrdin-val)<1e-10); % Compare within tolerance
[row,col,page] = ind2sub(size(frvrdin),idx);  % Get subscript indices

where row is the index into your first dimension, col into the second and finally page into the third.