Pre-allocating cell array of anonymous functions

2 次查看(过去 30 天)
Hi
I would apricate if someone can help me how to pre-allocate a cell array of anonymous functions that is later defined in a for loop. I do not what to run the loop backwards.
The for loop below exemplify what I want to do:
for q=1:q_tot
my_fun_array{q}=@(x)x^q;
end
best regard
Thomas

采纳的回答

Rik
Rik 2023-5-25
You can pre-allocate the cell array itself, but the contents will have to be set in the loop itself:
my_fun_array = cell(1,q_tot);
for q=1:q_tot
my_fun_array{q} = @(x)x^q;
end
Just an extra note: if you are planning to allow array inputs, you may want to use .^ instead.
  1 个评论
Thomas
Thomas 2023-5-25
编辑:Thomas 2023-5-25
Great, thanks!
I whas a bit hasty then I wrote the function in the exemplification.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by