필터 지우기
필터 지우기

How to dynamically change a function to solve a list of problems?

조회 수: 2 (최근 30일)
ELCIO S
ELCIO S 2019년 1월 24일
댓글: Guillaume 2019년 1월 25일
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에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by