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

New to MATLAB?

Create An Array That Contains All the Values of F(x)? Help!

Asked by Steven

Steven (view profile)

on 19 Sep 2013

I have a function f(x) that equals x if x < 1 and x^2 elsewhere.

How would I go about putting all the values of f(x) into another array, named y, for example?

1 Comment

Steven

Steven (view profile)

on 19 Sep 2013

If this helps x = linspace(-1,2,100).

Steven

Steven (view profile)

Products

No products are associated with this question.

3 Answers

Answer by Jan Simon

Jan Simon (view profile)

on 19 Sep 2013
Accepted answer

And to be complete:

y = x.^2;
y(x < 1) = x(x < 1);

0 Comments

Jan Simon

Jan Simon (view profile)

Answer by James Tursa

James Tursa (view profile)

on 19 Sep 2013

y = x;

y(y<1) = 1;

y = y.*x;

0 Comments

James Tursa

James Tursa (view profile)

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 19 Sep 2013
x=-5:5
y=(x<1).*x+(x>=1).*x.^2

0 Comments

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Contact us