Comparing characters in a matrix
Afficher commentaires plus anciens
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 commentaires
TAB
le 25 Fév 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
le 25 Fév 2013
Mini
le 25 Fév 2013
Réponse acceptée
Plus de réponses (1)
count=sum([M{:}]==tmp)
4 commentaires
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
le 25 Fév 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)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!