Count Unique Occurrences of Elements
Mostrar comentarios más antiguos
I have the following vectors:
Stops = [w w x x x y z ] %String
Buses = [A B C A C D B] % String
UniqueBuses = [A B C D] %String
I would like to have an output which tells me that:
Stop w has 1 A, 1 B.
Stop x has 2 C, 1 A.
Stop y has 1 D.
Stotp z has 1B.
Any help would be appreciated thank you!
Respuesta aceptada
Más respuestas (1)
per isakson
el 26 de Mayo de 2019
Editada: per isakson
el 26 de Mayo de 2019
I cannot make comments, thus I use an Answer

To ensure that Stops, Buses and UniqueBuses are row vectors add the following lines in the top of the script
Stops = reshape( Stops, 1,[] );
Buses = reshape( Buses, 1,[] );
UniqueBuses = reshape( UniqueBuses, 1,[] );
"If it helps the output can just be a matrix form instead of string form?" Not needed. These reshape() statements should solve the problem. They output row vectors for both row and column vectors.
1 comentario
Patrick Rungrugeecharoen
el 27 de Mayo de 2019
Thank you very much it works now.
Out of curiosity how would the code look if I wanted a header row consisting of UniqueBuses and the leading column to have UniqueStops then the matrix filled out with a bunch of numbers based on these results?
Categorías
Más información sobre String en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
