Simple MATLAB- how to find indices of max element in matrix?

In summary, to find the maximum element in a matrix using MATLAB, you can use the max function. This function takes in a matrix as its input and returns the maximum element along with its index. You can also find the maximum element along a specific dimension by using an additional argument. To only get the index of the maximum element, you can use the max function with an output argument. If there are multiple maximum elements, the max function will return the first occurrence. To get all the indices of the maximum elements, you can use the find function with a logical expression. It is important to handle empty matrices before using the max function to avoid errors.
  • #1
mikeph
1,235
18
Hi,

Say I have a 10x10 matrix, is there a simple code to finding the indices of the maximum value?

I'm trying to use max() but there doesn't appear to be any option of actually returning the index, rather than the value. I don't need the value, just the position.

Thanks,
Mike
 
Physics news on Phys.org
  • #2
This thread on MATLAB Central should give you a decent answer: http://www.mathworks.com/matlabcentral/newsreader/view_thread/170759
 

1. How do I find the maximum element in a matrix using MATLAB?

To find the maximum element in a matrix using MATLAB, you can use the max function. This function takes in a matrix as its input and returns the maximum element along with its index.

2. Can I find the maximum element along a specific dimension in a matrix?

Yes, you can use the max function with an additional argument specifying the dimension along which you want to find the maximum element. For example, max(matrix, [], 2) will return the maximum element in each row of the matrix.

3. How do I only get the index of the maximum element in a matrix?

You can use the max function with an output argument to only get the index of the maximum element. For example, [max_val, max_idx] = max(matrix) will return the maximum element in max_val and its index in max_idx.

4. What if there are multiple maximum elements in a matrix?

If there are multiple maximum elements in a matrix, the max function will return the first occurrence of the maximum element. To get all the indices of the maximum elements, you can use the find function with a logical expression matrix == max_val.

5. How do I handle empty matrices when using the max function?

If the input matrix is empty, the max function will return an empty matrix for both the maximum element and its index. It is recommended to check for empty matrices before using the max function to avoid errors in your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
6K
  • Programming and Computer Science
Replies
22
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
11K
  • Mechanical Engineering
Replies
2
Views
714
Back
Top