Finding the most common element in the first row of a matrix
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Josue Ortega
el 24 de Oct. de 2022
Comentada: Bruno Luong
el 24 de Oct. de 2022
Hi there
I have a matrix A, and I am wanting to take the first non zero element of each column. For example, for
A =[
1 2 0 1,
4 1 2 2,
2 3 4 3,
3 4 1 4
]
I want to have
1 2 2 3
and then find the most common element.
I looked at previous answers, and used the following code
c=arrayfun(@(x)find(A(:,x),1,'first'),1:size(A,2));
The above code works, but when I use it in a loop of size more than 10 it does not work anymore, giving me an error. I wonder if there is cleaner syntax to use here. Thanks.
1 comentario
Bruno Luong
el 24 de Oct. de 2022
"The above code works"
Sorry it looks like an arbitrary code to me.
Respuesta aceptada
Bruno Luong
el 24 de Oct. de 2022
Use mode command
[v,f]=mode([ 2 3 4 5 5 5 6 7 7])
3 comentarios
Bruno Luong
el 24 de Oct. de 2022
I don't like moving question. I't a waste of time for me. I'll deltete my answer no point to comment further.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!