How to dynamically change a function to solve a list of problems?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everybody. I have a list of problems that need to be solve by a list of algorithm. In fact, I have 28 problems where each of them needs to be solved by each of 10 algorithms. So I need create a routine that change automatically the name of problem and algorithm. How can I do it?
Let suppose, prob1, prob2, prob3, etc the problems who needs to be solved by the algorithms alg1, alg2, alg3, etc. Where alg1, alg2, alg3, etc are functions.
The problemas are given test problems. To solve, for example, the prob1 using the alg1, I need to do
[M, jM] = alg1(@prob1)
My question is, how can I dinamically change the names of algorithms and problems such that all the problems are solved by all the algorithms?
I try put all algorithms in a 'cell array' like
alg = {'alg1','alg2','alg3'}
and all the problems in another 'cell array' like
prob ={@prob1,@prob2,@prob3}
and then do
for i = 1:numel(alg)
for j = 1: numel(prob)
[M,jM]=(alg{i})(prob{j}) % I think here is the problem.
end
end
but it didn't work.
Can anyone help me?
Thank you.
0 commentaires
Réponse acceptée
Guillaume
le 24 Jan 2019
In some ways your question is strange because the solution is to use an array of functions handles instead of an array of function names. You're already using function handles for your problems (whatever these are) which is a bit unexpected. I would have thought that problems would be variables not functions.
alg = {@alg1, @alg2, @alg3}; %array of function handles
for i = 1:numel(alg)
for i = 1:numel(prob)
[M, jM] = alg{i}(prob{j}); %obviously you'd want to store the results in arrays/or cell arrays instead of overwriting them at each step of the loop
end
end
3 commentaires
Stephen23
le 25 Jan 2019
Modifié(e) : Stephen23
le 25 Jan 2019
When I used [a major internet search engine] to search for "MATLAB at symbol" these were the very first two results returned:
Both of those pages explain that the "at" symbol defines function handles, and they have links to the appropriate documentation.
Guillaume
le 25 Jan 2019
"I knew that the '@' needs to be used in 'prob' because there is it in the code of the problem, but at that time I didn't understand what its means"
It means that your problems are actually functions and what you are passing to the algorithms are the problem functions themselves. At some point the algorithm must call the function.
Plus de réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!