Inputting Numerical Matrix Values into Function Handle
이전 댓글 표시
Hello, I was wondering if anyone could help me with the following:
Taking a matrix, lets say [1 x 12] with all numerical values and inputting those values into a function handle such as:
for i = 1:12
j = @(x) array(i)*X^2 + array(i+1)*X + array(i+2)
plot j
hold on
end
답변 (1개)
No can do...
From the documentation on anonymous functions...
... the function handle includes those values. The values persist within the function handle even if you clear the variables:
To supply different values for the coefficients, you must create a new function handle: ...
In summary, the function handle structure stores internally the values of any variables used in the function that are not arguments to the function and they are then once the function handle exists associated w/ the function handle and there is no further connection w/ the previous variables so even whether they continue to exist or not is immaterial.
Only redefining the function will change them.
The code snippet you posted would (ignoring the case problem between 'x' in the dummy argument list and the 'X' in the statement expression) create a new anonymous function each pass thru the loop. The 'j' would call that function (again ignoring the missing argument to it problem) w/ the last set of coefficients.
There is, however, no real advantage I can see in doing such--if one is going to create/destroy the anonymous function on each usage it seems may as well just write the expression. I suppose there could be some case where it could be used in turn as an argument to another function such as a minimizer or somesuch.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!