How to dynamically change a function to solve a list of problems?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Guillaume
2019-1-24
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 个评论
Guillaume
2019-1-25
"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.
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!