Pre-allocating cell array of anonymous functions

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개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2023년 5월 25일

편집:

2023년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by