strings comparison with if then in array

Dear all,
Although I've already found a couple of posts on digits and strings together in cell arrays, I couldn't figure out how to explain my issue here.
I have a cell array alpha:
alpha = 383 x 8 cell
column 7 has strings ('miss' or 'incorrect'), the rest has integers.
I simply want to change the value in column 1 every time the correspondent value in column 7 has the label 'miss':
for x=1:383;
if alpha{x,7} == 'miss' | alpha{x,2} == 11; alpha{x,1} = 33; end
if ...
if ...
end
The error I get is:
??? Error using ==> eq MAtrix dimensions must agree
Is it referring to the fact that eq doesn't work for strings? I'm not so sure, because when I remove the for loop, and give a unique value to the x variable, then command works. Also, when I use the for loop, the x value in the Workspace shows the value 1, and not the requested 383. So I guess it is in the way I'm organizing the {} brackets.
I guess it's something silly I cannot see!
Any suggestions would be highly appreciated.
Sincerely,
Udiubu

 Akzeptierte Antwort

Conrad
Conrad am 12 Jul. 2012
Bearbeitet: Conrad am 12 Jul. 2012

1 Stimme

Hi Udiubu, you can use strcmp:
strcmp(alpha(x,7),'miss')
You can also get rid of the for loop by using logical indexing; your first test will be
strcmp(alpha(:,7),'miss')|cell2mat(alpha(:,2))==11
so the for loop is replaced by
alpha(logical(strcmp(alpha(:,7),'miss')|cell2mat(alpha(:,2))==11),1) = {33};

5 Kommentare

Ubu
Ubu am 12 Jul. 2012
See my comment below
Jan
Jan am 12 Jul. 2012
And to explain the problem explicitly: alpha{x,7} == 'miss' compares a [1 x N] char vector with a [1 x 4] char vector. When the contents of alpha{x,7} does not have 1 or 4 characters, the comparison must fail. As Conrad has said already: Compare strings by strcmp.
The issue is solved by using & instead of |
alpha(logical(strcmp(alpha(:,7),'miss') & cell2mat(alpha(:,2))==11),1) = {33};
Ubu
Ubu am 12 Jul. 2012
Thanks to both of you! Real precious help.
Sincerely,
Udiubu
Jan
Jan am 12 Jul. 2012
There is no need to cast the logical output of STRCMP to a logical.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ubu
Ubu am 12 Jul. 2012

0 Stimmen

Hi Conrad,
This is "almost" great!
As you can see for my previous post - I forgot to mention it in words - is that two conditions have to be meet on order to substitute thr value in column 1:
conditions are:
- I have a 'miss' in column 7 - I have the value 11 in column 2
Your last command seems to do so, it finds the "miss" values and subsitute the value in column 1, but regardless of the value in column 2 (the 11 value).
Is it correct to use "|" to satisfy both commands? Here the second condition in the if loop doensn't work.
Any additional hint would be great.
Now I also understood how logical works!
Thanks!
Udiubu

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

Ubu
am 12 Jul. 2012

Community Treasure Hunt

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

Start Hunting!

Translated by