Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the maximum or minimum of a multi-dimensional matrix in MATLAB? [duplicate]

I have a 4D array of measurements in MATLAB. Each dimension represents a different parameter for the measurement. I want to find the maximum and minimum value and the index (i.e. which parameter) of each.

What's the best way to do it? I figure I can take the max of the max of the max in each dimension, but that seems like a kludge.

like image 786
Nathan Fellman Avatar asked Apr 14 '10 05:04

Nathan Fellman


People also ask

How do you find the max of a 2d array in MATLAB?

M = max( A ) returns the maximum elements of an array. If A is a vector, then max(A) returns the maximum of A . If A is a matrix, then max(A) is a row vector containing the maximum value of each column of A .

How do you find the max and min value in MATLAB?

[ minA , maxA ] = bounds( A , 'all' ) computes the minimum and maximum values over all elements of A . This syntax is valid for MATLAB® versions R2018b and later. [ minA , maxA ] = bounds( A , dim ) operates along the dimension dim of A .

How do you find the maximum and minimum of a matrix?

We find maximum and minimum of matrix separately using linear search. Number of comparison needed is n2 for finding minimum and n2 for finding the maximum element. The total comparison is equal to 2n2. Pair Comparison (Efficient method):

How do you find the maximum value of indices of a matrix in MATLAB?

You can use max() to get the max value. The max function can also return the index of the maximum value in the vector. To get this, assign the result of the call to max to a two element vector instead of just a single variable.


1 Answers

Quick example:

%# random 4 d array with different size in each dim
A = rand([3,3,3,5]);

%# finds the max of A and its position, when A is viewed as a 1D array
[max_val, position] = max(A(:)); 

%#transform the index in the 1D view to 4 indices, given the size of A
[i,j,k,l] = ind2sub(size(A),position);

Finding the minimum is left as an exercise :).

Following a comment: If you do not know the number of dimensions of your array A and cannot therefore write the "[i,j,k,l] =" part, use this trick:

indices = cell(1,length(size(A)));

[indices{:}] = ind2sub(size(A),position);
like image 85
Adrien Avatar answered Oct 13 '22 15:10

Adrien