Comparing characters in a matrix
Ältere Kommentare anzeigen
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 Kommentare
TAB
am 25 Feb. 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
am 25 Feb. 2013
Mini
am 25 Feb. 2013
Akzeptierte Antwort
Weitere Antworten (1)
count=sum([M{:}]==tmp)
4 Kommentare
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
am 25 Feb. 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)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!