Defining function handles in a for loop

조회 수: 18 (최근 30일)
Yancheng Wang
Yancheng Wang 2020년 11월 7일
댓글: Walter Roberson 2020년 11월 8일
Hello everyone,
I have trying to define a series of function handles that are very similar. They only differ by their constant coefficients. They general idea is that I want to define functions s1 to s5 such that
for i =1:5
S_i = @(x) z(i)/3.*(p(i+1) -x)^3
end
where each s takes on their coefficeints from vectors z and p, as shown. But it seems like Matlab does not allow an array of function handles. What should I do?
Thanks a lot guys!
  댓글 수: 1
Stephen23
Stephen23 2020년 11월 7일
"You can create an array of function handles by collecting them into a cell or structure array."

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

답변 (1개)

Walter Roberson
Walter Roberson 2020년 11월 7일
for i =1:5
S_i{i} = @(x) z(i)/3.*(p(i+1) -x)^3
end
  댓글 수: 6
Yancheng Wang
Yancheng Wang 2020년 11월 7일
편집: Yancheng Wang 2020년 11월 7일
Thank you! I did not know this and it helps!
So, as I defined the functions as above, when I try to graph them, matlab indicates that these are "array inputs" and should be "properly vectorized". Do you know what happened/what I should do to graph these functions?
Thanks again
Walter Roberson
Walter Roberson 2020년 11월 8일
S_i{i} = @(x) z(i)/3.*(p(i+1) -x).^3 %notice .^ instead of ^

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by