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

 Risposta accettata

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 Commento

Thomas
Thomas il 25 Mag 2023
Modificato: Thomas il 25 Mag 2023
Great, thanks!
I whas a bit hasty then I wrote the function in the exemplification.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2022b

Richiesto:

il 25 Mag 2023

Modificato:

il 25 Mag 2023

Community Treasure Hunt

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

Start Hunting!

Translated by