# Create function such that for each element of the vector it applies different function

조회 수: 1(최근 30일)
Dawid Wysocki 2020년 12월 30일
댓글: Ameer Hamza 2020년 12월 30일
Let's say I have following functions:
f1 = @(x) a*x(1) + 1;
f2 = @(x) b*x(2) - 1;
where a and b are parameters that I want to specify before. And a vector:
x = [2;4];
I want to create function handler F that will be created before hand (like a delegation in C#) using a and b. I want to use F to calculate next vector values (f1 for first cell, f2 for second) for instance given a = b = 1:
F(x) = [3;3];
I tried creating cell:
F = {f1;f2}
but it gives me an error "Index exceeds the number of array elements (2).".

댓글을 달려면 로그인하십시오.

### 채택된 답변

Ameer Hamza 2020년 12월 30일
편집: Ameer Hamza 2020년 12월 30일
Try something like this
a = 1;
b = 1;
x = [2; 4];
f1 = @(x) a*x(1) + 1;
f2 = @(x) b*x(2) - 1;
F = @(x) [f1(x); f2(x)];
Result
>> F(x)
ans =
3
3
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Ameer Hamza 2020년 12월 30일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

### 범주

Find more on .NET Events and Delegates in MATLAB in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by