Comparing characters in a matrix

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 件のコメント

TAB
TAB 2013 年 2 月 25 日
Please explain with the help of example of data and your expected output.
Mini
Mini 2013 年 2 月 25 日
編集済み: Mini 2013 年 2 月 25 日
M =
'U'
'C'
'Y'
'H'
'O'
tmp = 'U'
I want to increment the variable 'count' every time a character in M matches that in tmp.
Matt J
Matt J 2013 年 2 月 25 日
編集済み: Matt J 2013 年 2 月 25 日
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
Mini 2013 年 2 月 25 日
cmd = ['tesseract ',file,' tmp -psm 10']; %this is a command to run OCR and will save the character returned in a tmp.txt file
system(cmd);
%save temp results
f2 = fopen('tmp.txt','r');
tmp = fgets(f2); %this is the tmp character I want to compare against
if strcmp(M(i),tmp)
count = count+1;
end; if true
% code
end
Mini
Mini 2013 年 2 月 25 日
oh dear I realised my mistake... its not working because the characters in M are stored with the apostrophes in the format: 'U','C','Y'... but in the variable tmp, the character are just stored as: U, C, Y... is there a way to solve this kind of problem?

サインインしてコメントする。

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 25 日
編集済み: Azzi Abdelmalek 2013 年 2 月 25 日

0 投票

strcmp(M(i),tmp)

9 件のコメント

Mini
Mini 2013 年 2 月 25 日
doesn't workk :(
Matt J
Matt J 2013 年 2 月 25 日
編集済み: Matt J 2013 年 2 月 25 日
Azzi's code will work fine if M is truly a matrix as you claim. The following example below proves this
>> M=['UCYHO'].'; tmp='U';
>> strcmp(M(1),'U')
ans =
1
I again suspect, however, that M is a cell array and that you do not understand the difference between cell arrays and matrices.
Mini
Mini 2013 年 2 月 25 日
Hey, I tried using 'U' instead of tmp and stepped through the code and figured that it works that way... the problem is because the character in tmp does not have the apostrophe so its just U instead of 'U'. Do you know how to solve this?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 25 日
編集済み: Azzi Abdelmalek 2013 年 2 月 25 日
Mini, to make it simple, give a sample of M, and tmp, and type
whos tmp
whos M
and tell us what you've got
Mini
Mini 2013 年 2 月 25 日
M(1) = 'U' tmp = U
thats why strcmp(M(1),tmp) returns false, but when I try strcmp(M(1),'U') it returns true.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 25 日
Type
whos tmp
whos M
and tell what you've got
Mini
Mini 2013 年 2 月 25 日
編集済み: Mini 2013 年 2 月 25 日
>> whos tmp
Name Size Bytes Class Attributes
tmp 1x2 4 char
>> whos M
Name Size Bytes Class Attributes
M 257x1 15936 cell
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 25 日
編集済み: Azzi Abdelmalek 2013 年 2 月 25 日
The problem is tmp is not equal to 'U' but to something like
'U ' % with spaces
Try this to remove space from tmp
strcmp(M(1),strtrim(tmp))
Mini
Mini 2013 年 2 月 25 日
omg this works, thank you sooooooooo much!!! (: Hope you have a great week ahead!! (:

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2013 年 2 月 25 日
編集済み: Matt J 2013 年 2 月 25 日

0 投票

count=sum([M{:}]==tmp)

4 件のコメント

Mini
Mini 2013 年 2 月 25 日
編集済み: Mini 2013 年 2 月 25 日
but M is a matrix, and i need to access only one index at a time to compare against tmp
Matt J
Matt J 2013 年 2 月 25 日
編集済み: Matt J 2013 年 2 月 25 日
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
Mini 2013 年 2 月 25 日
oh thank you for the explanation... now i understand where you are coming from.. but this tmp changes value in every iteration, so in that case I will have to check it in every iteration right?
Matt J
Matt J 2013 年 2 月 25 日
編集済み: Matt J 2013 年 2 月 25 日
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)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by