Efficient Way to find index of max column for each row
64 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hey I've an matrix of size n x m. I want to find the index of max_col corresponding to each row. I know, i can use a loop to check but is there any other efficient way to do that?
thanks in advance.
0 comentarios
Respuestas (4)
Les Beckham
el 12 de Jul. de 2022
Editada: Les Beckham
el 12 de Jul. de 2022
Are you wanting to find the column number of the maximum value in each row? If so, this is how you can do that:
A = magic(5) % example data
[m, idx] = max(A, [], 2) % max value from each row and column number where the max value occurs
If not, please clarify what you are trying to do.
0 comentarios
Keshav
el 12 de Jul. de 2022
Hi, Based on my understanding you have a matrix. for each row you want to find the index of maximum value. you can use the below code to do that.
x = [1 2 3; 5 3 4;10 8 9]
[mx,idx] = max(x,[],2)
here idx(i) will represent the index of column with max value in ith row.
0 comentarios
Harshit Gupta
el 12 de Jul. de 2022
Hi, I understand that you're trying to find an efficient way to find the maximum values in each row.
You can do this simply with the max() function on the matrix without using a for loop.
Here's an example:
M=[2 4 0 1;12 3 5 2;7 10 4 1;3 5 20 7]
[max_values, idx] = max(M') %row maximums
You can even get the column maximums and the maximum value of the matrix
[max_values, idx] = max(M) %col maximums
[matrix_max, idx] = max(M(:)) %matrix maximum
Hope this helps!
0 comentarios
Anay Aggarwal
el 12 de Jul. de 2022
Hi Varsha,
I have an understanding that you want to find the column number of maximum element in each row of a matrix.
You can perform the following operation:
x = [1 2 3; 4 50 6;11 8 9]
[mx,idx] = max(x,[],2)
Hope this helps
Regards
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!