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:
Vector comparison using if

Subject: Vector comparison using if

From: Rajasekhara Madhusudan Narayana Bhatla

Date: 20 Nov, 2012 01:52:09

Message: 1 of 5

Hi,
I'm sought of new using matlab. I've a situation where I've to assign a variable 2 different values based on how the values in vector

it is something like.
fs=1000;
x=1:4/fs:5;
if x<2
Y=x.^2
else
Y=4;
end.

i.e, my Y will also be a vector whose value depends on where I x is. if x is <2 then y is x^2 if not it is a constant.

I'm not sure how to do this.
i tried it with if any(x)<2 but I then i realized that it is not correct. since I want for all values of x<2 Y=x^2 and all values of x >2 x = 4.

Please Help,

Thanks,
Sekhar.

Subject: Vector comparison using if

From: Nasser M. Abbasi

Date: 20 Nov, 2012 02:30:55

Message: 2 of 5

On 11/19/2012 7:52 PM, Rajasekhara Madhusudan Narayana Bhatla wrote:
> Hi,
> I'm sought of new using matlab. I've a situation where I've to assign a variable 2 different values based on how the values in vector
>
> it is something like.
> fs=1000;
> x=1:4/fs:5;
> if x<2
> Y=x.^2
> else
> Y=4;
> end.
>
> i.e, my Y will also be a vector whose value depends on where I x is. if x is <2
>then y is x^2 if not it is a constant.
>
> I'm not sure how to do this.
> i tried it with if any(x)<2 but I then i realized that it is not correct.
>since I want for all values of x<2 Y=x^2 and all values of x >2 x = 4.
>
> Please Help,
>
> Thanks,
> Sekhar.
>

many ways to do this. One way

----------------
fs = 1000;
x = 1:4/fs:5;
Y = 4*ones(length(x),1);
Y(x<2) = x(x<2).^2
------------------

--Nasser

Subject: Vector comparison using if

From: Rajasekhara Madhusudan Narayana Bhatla

Date: 20 Nov, 2012 05:33:08

Message: 3 of 5

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k8eq0v$u39$1@speranza.aioe.org>...
> On 11/19/2012 7:52 PM, Rajasekhara Madhusudan Narayana Bhatla wrote:
> > Hi,
> > I'm sought of new using matlab. I've a situation where I've to assign a variable 2 different values based on how the values in vector
> >
> > it is something like.
> > fs=1000;
> > x=1:4/fs:5;
> > if x<2
> > Y=x.^2
> > else
> > Y=4;
> > end.
> >
> > i.e, my Y will also be a vector whose value depends on where I x is. if x is <2
> >then y is x^2 if not it is a constant.
> >
> > I'm not sure how to do this.
> > i tried it with if any(x)<2 but I then i realized that it is not correct.
> >since I want for all values of x<2 Y=x^2 and all values of x >2 x = 4.
> >
> > Please Help,
> >
> > Thanks,
> > Sekhar.
> >
>
> many ways to do this. One way
>
> ----------------
> fs = 1000;
> x = 1:4/fs:5;
> Y = 4*ones(length(x),1);
> Y(x<2) = x(x<2).^2
> ------------------
>
> --Nasser

Ok...got it...
Thanks a lot.

if possible can you give me some pointers as to what are other ways.

Thanks,
Sekhar.

Subject: Vector comparison using if

From: Nasser M. Abbasi

Date: 20 Nov, 2012 08:14:21

Message: 4 of 5

On 11/19/2012 11:33 PM, Rajasekhara Madhusudan Narayana Bhatla wrote:

>> many ways to do this. One way
>>
>> ----------------
>> fs = 1000;
>> x = 1:4/fs:5;
>> Y = 4*ones(length(x),1);
>> Y(x<2) = x(x<2).^2
>> ------------------
>>
>> --Nasser
>
> Ok...got it...
> Thanks a lot.
>
> if possible can you give me some pointers as to what are other ways.
>

another way:

-------------------------
fs = 1000;
x = 1:4/fs:5;
Y = (x.^2).*(x<2)+4*(x>=2)
-----------------------

--Nasser

Subject: Vector comparison using if

From: Jos (10584)

Date: 20 Nov, 2012 09:04:09

Message: 5 of 5

"Rajasekhara Madhusudan Narayana Bhatla" <rmnaraya@asu.edu> wrote in message <k8eno8$k9d$1@newscl01ah.mathworks.com>...
> Hi,
> I'm sought of new using matlab. I've a situation where I've to assign a variable 2 different values based on how the values in vector
>
> it is something like.
> fs=1000;
> x=1:4/fs:5;
> if x<2
> Y=x.^2
> else
> Y=4;
> end.
>
> i.e, my Y will also be a vector whose value depends on where I x is. if x is <2 then y is x^2 if not it is a constant.
>
> I'm not sure how to do this.
> i tried it with if any(x)<2 but I then i realized that it is not correct. since I want for all values of x<2 Y=x^2 and all values of x >2 x = 4.
>
> Please Help,
>
> Thanks,
> Sekhar.

Learn about logical indexing, one of the great strengths of matlab. Here is an example:

x = 1:10
tf = x < 5 % a logical vector
y = zeros(size(x)) % pre-allocation
y(tf) = 1
y(~tf) = 10

~ Jos

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