Function that makes Anonymous Functions
古いコメントを表示
I want to create a MATLAB function that takes a set of data, fits a curve with the least error, and returns the fitted curve as an anonymous function. When I attempted this, the anonymous function contained variables instead of numerical coefficients. Is there a way to modify the function so that it generates an anonymous function with specific numerical coefficients?
1 件のコメント
James Tursa
2023 年 12 月 12 日
Please post a small example that reproduces the problem.
採用された回答
その他の回答 (2 件)
Did you try something like this:
x = [5, 10];
y = [15, 25];
% Get parameters.
[a, b] = GetFit(x, y)
% Define anonymous function
f = @(x) a*x+b
% Call anonymous function with a value of 7.5
yFit = f(7.5)
%=======================================================
function [a, b] = GetFit(x, y)
coefficients = polyfit(x, y, 1);
a = coefficients(1);
b = coefficients(2);
end
Walter Roberson
2023 年 12 月 12 日
When an anonymous function is created and the function has reference to variables that are not named parameters, then MATLAB inserts the names of the variables into the anonymous function. MATLAB also associates a property named workspace with the anonymous function, which is a scalar cell array. Inside the scalar cell array, there is a struct with one field for each "captured" variable, and with the associated value.
When anonymous functions are created, MATLAB makes no attempt to substitute values for variables that are used in the function. It does not even examine the values to determine their class: all it looks for is whether the name is defined as a variable in scope, and if so creates a copy-on-write reference to the name.
MATLAB will not attempt to substitute the text equivalent of variables for variable names.
If MATLAB were to attempt to substitute the text equivalent of variables for variable names, it would at most be able to do so for objects that could be exactly duplicated by plain text constructor calls, such as classes that do not have methods that can assign to class properties, with all of the initialization being in the class initiazation or the class constructor. Numeric classes are potentially do-able -- but remember that MATLAB does not have any direct syntax for constructing numeric arrays with more than 2 dimensions, so the text that would have to be emitted for 3 or more dimensions would have to include calls to cat() along dimensions of smaller pieces constructed with [] calls. That would get a bit messy fairly quickly. And what if the variable is fairly large?
The alternative that would allow MATLAB to encode more types of variables would be if the way MATLAB encoded variables was that it exposed its internal interface to serialize and deserialize objects https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data . The way that MATLAB sends objects between parallel clients and workers is that it serializes the objects into text and deserializes them on the other side. Serialization of objects contains information about how to recreate some relatively complicated objects.
Unfortuantely, serialized objects are unreadable by humans. So it is not clear that it would help anything for MATLAB to convert an anonymous function like @(x) x + A into
@(x) x + deserialize('yPdph0W.026c2QMiNKhbFNDGsRgYxLNlt1spYRcLn6Sme01TENS3NOJwUBJrdgdGt0NbxTRfmOTfleo262ArZByYrKEiXI19oXQ6e8bBVLD69tk0pFurMK9OJqHsCMMb')
It might have converted the objects into printable text instead of just encoding the object names, but have you gained anything of worth?
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!