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.

采纳的回答

Guillaume
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 个评论
Stephen23
Stephen23 2019-1-25
编辑:Stephen23 2019-1-25
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
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 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by