Comparing characters in a matrix
Mostrar comentarios más antiguos
I have a (nx1) matrix,M, that stores a list of characters and I need to iterate through a loop and keep comparing the i-th character in the matrix with a temp character.
But I'm not sure how to do this, i tried using M{i,1}== tmp and strcmp(M{i,1},tmp) but neither seem to work. Please help, thank you so much!
5 comentarios
TAB
el 25 de Feb. de 2013
Please explain with the help of example of data and your expected output.
In what you've shown, M is a cell array of strings, not a matrix. If M is always nx1, it would indeed make more sense to convert it to a matrix and maintain it that way
M=[M{:}].'
Mini
el 25 de Feb. de 2013
Mini
el 25 de Feb. de 2013
Respuesta aceptada
Más respuestas (1)
count=sum([M{:}]==tmp)
4 comentarios
But your code shows that all you're doing is counting occurrences of tmp. That's also what you stated earlier that you wanted to do
I want to increment the variable 'count' every time a character in M matches that in tmp.
If this is all you are doing, then my code does that for you in one line. If M really is a matrix and not a cell array, then my proposal would simplify to
count=sum(M==tmp)
However, your example clearly shows M to be a cell array.
Mini
el 25 de Feb. de 2013
Not if there's a vectorized way of creating tmp. If M and tmp are string vectors of the same length, you can still do
count=sum(M==tmp)
Categorías
Más información sobre Matrix Indexing 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!