필터 지우기
필터 지우기

How to I create a function handle through recursion?

조회 수: 1 (최근 30일)
Nathan Phelps
Nathan Phelps 2020년 10월 26일
댓글: Nathan Phelps 2020년 10월 26일
I want to output a polynomial using values from a vector. Kind of like how you'd add recursively for a running sum, I'm trying to add terms recursively.
If my function is f(t) and A is a matrix of values, I'd want f(t) to be:
f(t) = A(1,1) + A(2,1) * t^1 + A(3,1) * t^2 + ... + A(n,1) * t^(n-1)
Edit: I forgot to add that I'm calculating the A matrix in the function, so the length isn't known, hence why I need to create the function recursively.

채택된 답변

Stephen23
Stephen23 2020년 10월 26일
편집: Stephen23 2020년 10월 26일
I don't see any reason why you need any recursion, basic vectorized code works just fine:
V = 0:size(A,1)-1;
F = sum(A(:,1).*t.^V(:))
  댓글 수: 3
Stephen23
Stephen23 2020년 10월 26일
>> A = randi(9,5,3)
A =
7 8 4
1 7 4
3 3 7
1 9 8
1 1 2
>> V = 0:size(A,1)-1;
>> fun = @(t) sum(A(:,1).*t.^V(:));
>> fun(2)
ans =
45
>> fun(5)
ans =
837
Nathan Phelps
Nathan Phelps 2020년 10월 26일
I read my initial output wrong, thanks for the help!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by