Thread Subject: How do i simplify this statment??

Subject: How do i simplify this statment??

From: John

Date: 25 Jul, 2005 13:20:45

Message: 1 of 8

if test == 'Snr04'
        if run == '5'
            control = 'gk';
        elseif run == '422'
            control = 'gk';
        elseif run == '423'
            control = 'gk';
        elseif run == '424'
            control = 'gk';
        else
            control = '';
        end
    else
        control = '';
    end

I guess what i need to ask is how can i use && or || using strings??

Subject: How do i simplify this statment??

From: Alex V.

Date: 25 Jul, 2005 19:38:40

Message: 2 of 8

Hi John,

John wrote:
> if test == 'Snr04'
> if run == '5'
> control = 'gk';
> elseif run == '422'
> control = 'gk';
> elseif run == '423'
> control = 'gk';
> elseif run == '424'
> control = 'gk';
> else
> control = '';
> end
> else
> control = '';
> end
>
> I guess what i need to ask is how can i use && or || using strings??

this should work:

control = '';
str = {'5' '422' '423' '424'};
if test == 'Snr04' & ~isempty(strmatch(runn, str, 'exact'))
        control = 'gk';
end;

BTW, i replaced 'run' with 'runn', since run is a matlab command.

Hth,
-Alex.
--
email address is valid but messages will be deleted (due to spam)
for serious mail, please replace 'nospam' with 'usenet'

Subject: How do i simplify this statment??

From: LRM1138

Date: 25 Jul, 2005 11:32:13

Message: 3 of 8

That's a nice and short solution, but to directly answer the question -
you can actually just use the single operators & and |:

%%%% test and runn defined %%%%
tic
if test=='Snr04'
     if runn=='5' | runn=='422' | runn=='423' | runn=='424'
          control = 'gk';
     else
          control = '';
     end
else
     control = '';
end
toc
%%%%%%

Or, borrowing a shortcut from Alex's solution:

%%%% test and runn defined %%%%
tic
control='';
if test=='Snr04'
     if runn=='5' | runn=='422' | runn=='423' | runn=='424'
          control = 'gk';
     end
end
toc
%%%%%%

Elapsed time is 0.010000 seconds.
Elapsed time is 0.000000 seconds.

In the help file it says you need the expressions to evaluate as
scalars to use || and &&, whereas | and & work on arrays (hence the
name "elementwise"). So I assume the strings are treated as arrays and
compared that way.

Subject: How do i simplify this statment??

From: gigio

Date: 25 Jul, 2005 18:56:07

Message: 4 of 8

On Mon, 25 Jul 2005 19:38:40 +0200, "Alex V." <alex.nospam@arcor.de>
wrote:

[...]

>
>this should work:
>
>control = '';
>str = {'5' '422' '423' '424'};
>if test == 'Snr04' & ~isempty(strmatch(runn, str, 'exact'))
> control = 'gk';
>end;
>


I am surprise about your solution!
I believe that it need
strcmp()
in order to compare strings

or, at least,
all( test == 'Snr04' )
if the strings are treated as arrays

Subject: How do i simplify this statment??

From: LRM1138

Date: 25 Jul, 2005 13:03:57

Message: 5 of 8

gigio wrote:
> On Mon, 25 Jul 2005 19:38:40 +0200, "Alex V." <alex.nospam@arcor.de>
> wrote:
>
> [...]
>
> >
> >this should work:
> >
> >control = '';
> >str = {'5' '422' '423' '424'};
> >if test == 'Snr04' & ~isempty(strmatch(runn, str, 'exact'))
> > control = 'gk';
> >end;
> >
>
>
> I am surprise about your solution!
> I believe that it need
> strcmp()
> in order to compare strings
>
> or, at least,
> all( test == 'Snr04' )
> if the strings are treated as arrays

Ah yes, it seems that I often give less-than-adequate advice. In order
for that to work, the strings defined as "test" and "runn" have to be
the same length as the conditional string or else it will return an
error. That is, setting test='Snr041' gives the error:

??? Error using ==> eq
Array dimensions must match for binary array op.

Error in ==>
if test=='Snr04'

 I suppose you could set it up so the logical test returns false on an
error, then it would work everytime. Or something like:

control='';
if length(test)==length('Snr04')
     if test=='Snr04'
          ...

so it would completely skip the loop if the lengths don't match. And
don't forget something similar for the "runn" string too. But, as
gigio says, using strcmp is probably the best solution:

a={'5';'422';'423';'424'};
control='';
if strcmp(test,'Snr04')
     if any(strcmp(runn,a))
          control='gk';
     end
end

NOW it should handle whatever you throw at it.

Subject: How do i simplify this statment??

From: Alex V.

Date: 26 Jul, 2005 09:42:58

Message: 6 of 8

gigio wrote:
> On Mon, 25 Jul 2005 19:38:40 +0200, "Alex V." <alex.nospam@arcor.de>
> wrote:
>
> [...]
>
>
>>this should work:
>>
>>control = '';
>>str = {'5' '422' '423' '424'};
>>if test == 'Snr04' & ~isempty(strmatch(runn, str, 'exact'))
>> control = 'gk';
>>end;
>>
>
>
>
> I am surprise about your solution!
> I believe that it need
> strcmp()
> in order to compare strings

I'm not sure about that. The statement
test == 'Snr04' should work. Even if test isn't a string. Of course,
using strcmp would be more clear. I just copied this expression from the
OP John.

>
> or, at least,
> all( test == 'Snr04' )
> if the strings are treated as arrays

-Alex.

--
email address is valid but messages will be deleted (due to spam)
for serious mail, please replace 'nospam' with 'usenet'

Subject: How do i simplify this statment??

From: Jerome

Date: 26 Jul, 2005 04:03:21

Message: 7 of 8

Alex V. wrote:
>
> I'm not sure about that. The statement
> test == 'Snr04' should work.

Try this :

a='aaa'
strcmp(a,'aa')

a='aaa'
a=='aaa'

Now try this one :

a='aaa'
a=='aa'

You have to use strcmp if the number of character in the string is
not constant.
See PB and Titus answers there :
Mike Szabo, "Help with logical operators" #, 25 Jul 2005 1:05 pm </WebX?14@@.ef0ecd0>


Jérôme

Subject: How do i simplify this statment??

From: Alex V.

Date: 26 Jul, 2005 13:57:14

Message: 8 of 8

Jérôme wrote:
> Alex V. wrote:
>
>>I'm not sure about that. The statement
>>test == 'Snr04' should work.
>
>
> Try this :
>
> a='aaa'
> strcmp(a,'aa')
>
> a='aaa'
> a=='aaa'
>
> Now try this one :
>
> a='aaa'
> a=='aa'
>
> You have to use strcmp if the number of character in the string is
> not constant.

Unless a has only one value. Then it works just fine. ;-)
a = 'a'
a == 'aaa'


> See PB and Titus answers there :
> Mike Szabo, "Help with logical operators" #, 25 Jul 2005 1:05 pm </WebX?14@@.ef0ecd0>


I'm aware of that. I've never said, that it would work for *every*
possible value of 'test'. As I said, I've copied this from John, the OP
hoping that this statement would work for all values, his 'test' may have.

Though - and to make that clear, using strcmp would be a more robust and
better solution than my quick-and-dirty one.

> Jérôme

-Alex.


--
email address is valid but messages will be deleted (due to spam)
for serious mail, please replace 'nospam' with 'usenet'

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com