Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Basic Question

Subject: Basic Question

From: C Meek

Date: 16 Nov, 2010 19:25:04

Message: 1 of 5

I'd like to write a Matlab command that does the following:

f(x)={ 2, x>=3
       { 3, x<3

Is the best way to do this using if..else statements or is there a better way?

Thanks for any help

Subject: Basic Question

From: Sean de

Date: 16 Nov, 2010 19:34:04

Message: 2 of 5

"C Meek" <meeky2000@hotmail.com> wrote in message <ibulqg$d49$1@fred.mathworks.com>...
> I'd like to write a Matlab command that does the following:
>
> f(x)={ 2, x>=3
> { 3, x<3
>
> Is the best way to do this using if..else statements or is there a better way?
>
> Thanks for any help

Better than if statements because it's vectorized (you can pass in a matrix or vector instead of a scalar)
%%%
x(x>=3) = 2;
x(x<3) =3;

Subject: Basic Question

From: C Meek

Date: 16 Nov, 2010 19:46:04

Message: 3 of 5

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ibumbc$h9r$1@fred.mathworks.com>...
> "C Meek" <meeky2000@hotmail.com> wrote in message <ibulqg$d49$1@fred.mathworks.com>...
> > I'd like to write a Matlab command that does the following:
> >
> > f(x)={ 2, x>=3
> > { 3, x<3
> >
> > Is the best way to do this using if..else statements or is there a better way?
> >
> > Thanks for any help
>
> Better than if statements because it's vectorized (you can pass in a matrix or vector instead of a scalar)
> %%%
> x(x>=3) = 2;
> x(x<3) =3;

Thanks!

Subject: Basic Question

From: dpb

Date: 16 Nov, 2010 19:51:53

Message: 4 of 5

Sean de wrote:
> "C Meek" <meeky2000@hotmail.com> wrote in message
> <ibulqg$d49$1@fred.mathworks.com>...
>> I'd like to write a Matlab command that does the following:
>>
>> f(x)={ 2, x>=3
>> { 3, x<3
>>
>> Is the best way to do this using if..else statements or is there a
>> better way?
>>
>> Thanks for any help
>
> Better than if statements because it's vectorized (you can pass in a
> matrix or vector instead of a scalar)
> %%%
> x(x>=3) = 2;
> x(x<3) =3;

Excepting that the first substitution potentially changes the results
since anything that was operated on in but wouldn't have been before now
will be...

x =
      1 2 3 4 5
 >> x(x>=3)=2; x(x<3)=3
x =
      3 3 3 3 3
 >> x=1:5
x =
      1 2 3 4 5
 >> idx1=x(x>=3); idx2=x(x<3);
 >> x(idx1)=2;x(idx2)=3
x =
      3 3 2 2 2
 >>

--

Subject: Basic Question

From: Walter Roberson

Date: 16 Nov, 2010 20:00:08

Message: 5 of 5

On 10-11-16 01:34 PM, Sean de wrote:
> "C Meek" <meeky2000@hotmail.com> wrote in message
> <ibulqg$d49$1@fred.mathworks.com>...
>> I'd like to write a Matlab command that does the following:
>>
>> f(x)={ 2, x>=3
>> { 3, x<3
>>
>> Is the best way to do this using if..else statements or is there a
>> better way?
>>
>> Thanks for any help
>
> Better than if statements because it's vectorized (you can pass in a
> matrix or vector instead of a scalar)
> %%%
> x(x>=3) = 2;
> x(x<3) =3;

That isn't going to work. Suppose you start with x(7) = 4. Then the first
subclause x>=3 is going to be true at position 7, so you would set that
position to 2. Then when you got to the second statement, x < 3 would now be
true for that 7th position because the location get set to 2 a moment ago, so
the location would get set to 3. The end result would be x(7) being 3 whereas
clearly your description would want it to end up being 2.

You could use

2 + (x<3)

as the expression. That does have a problem, but only with respect to a result
you did not define. If one of the locations is NaN, then x<3 will be false,
and you would generate 2 for that location, implying that NaN >= 3 which is
also false. NaN does not match either of your conditions, so f(NaN) is
undefined for you rather than having a particular value. Fixing this gets obscure.

Tags for this Thread

No tags are associated with this thread.

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.

Contact us