Valentin Tihomirov
Valentin Tihomirov

Reputation: 1

How do I map every element in a range with a function?

I see that I can define a range with 1:3. I can also apply a function to a number, e.g. sin(1). But, how do I map the range of numbers with the function? I would like to $1,2,3 \to sin(1), sin(2), sin(3)$.

Upvotes: -1

Views: 55

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 14939

I suspect you want something like this:

[1:n; sin(1:n)].'
ans =
    1.0000    0.8415
    2.0000    0.9093
    3.0000    0.1411
    4.0000   -0.7568

or

f = @(n) sin(n)
f(1:n)
ans =
    0.8415    0.9093    0.1411   -0.7568

I can explain it more thoroughly if you confirm that this is what you want to achieve.

Upvotes: 1

Related Questions