find location of max value in array matlab
To find the location of the maximum value in an array in MATLAB, you can use the max
function along with the find
function. Here's an example:
array = [1, 5, 3, 9, 2];
[maxValue, maxIndex] = max(array);
In this example, the max
function is used to find the maximum value in the array, and the find
function is used to find the indices where the maximum value occurs. The maxValue
variable will store the maximum value, and the maxIndex
variable will store the indices of the maximum value in the array.
If you want to find the indices of all occurrences of the maximum value, you can modify the code as follows:
array = [1, 5, 3, 9, 2];
[maxValue, maxIndices] = max(array);
maxIndices = find(array == maxValue);
In this modified code, the find
function is used to find all the indices where the array is equal to the maximum value.
Please note that MATLAB uses 1-based indexing, so the indices start from 1.