Defining particular values of a function

Hi,
so I'm an absolute beginner and I have the following problem: I want a function which looks something like this:
f(1)=1, f(n)=0 for n =/= 1.
My attempt
if n=1
f=1
else
f=0
returns the error:
The expression to the left of the equals sign is not a valid target for an assignment.
(where they refer to the "n")
So: what is the problem with that and how is it done? Also, how should I go about that if I have multiple particular values I want to define?
Thanks in advance :)

 採用された回答

Honglei Chen
Honglei Chen 2013 年 3 月 14 日

0 投票

Since you are comparing the number, it should be
n==1

2 件のコメント

Georg
Georg 2013 年 3 月 14 日
Thanks!
Say I want to define f(1),f(2),f(3) seperately.
Is there a clean way to do that (without using if else)?
Honglei Chen
Honglei Chen 2013 年 3 月 14 日
Say you need 10 of them
f = zeros(10,1);
f(1) = 1;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by