Can I create several similar anonymous functions with loop and array
显示 更早的评论
I want to create several similar anonymous functions, which depends on elements of an array.
An array is given as an input. Both the length of array and the elements of array are unknown before running the code. For example,
a=[1,2,3]
I want to create anonymous functions ( actually what i want is some other functions defined based on the input array a, but here for simplicity I use polynomial as an example)
f1=1,f2=x, f3=x^2,f4=x^3
and then apply them to other inputs with other calculations. Since I am new to Matlab, the only way I can think of is using a for-loop
temp=cell(1,size(a,2));
temp{1,1}=1;
for i=2:size(a,2)
temp{1,i}=@(x) x.^a(i);
end
But after running the code, I found a(i) actually doesn't become a value when the code is run. And the anonymous functions are hard-coded as x^a(i) during the loop, which is not what I want.
So given an array with arbitrary length or a matrix with arbitrary size, how can I dynamically create several similar anonymous functions depending on the entries of the array?
I hope I express my question clear enough. Thank you.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!