uncertain number of loop structures

조회 수: 1 (최근 30일)
Liang
Liang 2011년 5월 5일
Here is my problem:
the function has uncertain number of inputs, while the number of loop structures depends on the number of inputs. For example, if there is only one input, then only one loop structure is needed, two inputs, two loop structures, ...
I know how to deal with uncertain inputs, but need help to code uncertain number of loop structures.
Leon
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2011년 5월 5일
What are the loops doing? It's quite possible to avoid the loops altogether which makes the whole problem irrelevant. Paste a snippet of code and the problem for us to see!
Walter Roberson
Walter Roberson 2012년 8월 24일
(Re-opened as the question is of sufficient general interest and the answers give enough information to be useful.)

댓글을 달려면 로그인하십시오.

답변 (4개)

the cyclist
the cyclist 2011년 5월 5일
I agree with Sean de, that you may be able to avoid the loops with some more clever programming, so you might want to post some code.
However, once upon a time, I had to do exactly what you are asking. It can be done with the eval() function. Here is a trivial example that will give you the idea:
NDIM = 4;
forString = [];
doString = [];
endString = [];
for nd = 1:NDIM
forString = ['for i',num2str(nd),'=1:10,',forString];
doString = ['disp(i',num2str(nd),');',doString];
endString = [endString,'end;']; %#ok<AGROW>
end
loopString = [forString,doString,endString];
eval(loopString);
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2011년 5월 5일
Of course to avoid using evil eval, if you know what the maximum number of loops possibly necessary is, you could just have that many written out and skip all of the interior ones you don't need.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2011년 5월 5일
Or one could use fprintf along the same lines as The Cyclist's suggestion to generate a custom m-file for each number of dimensions applicable. Then when the user enters the number of dimensions that m-file is called.

Liang
Liang 2011년 5월 5일
This is what I am trying to do:
p is time series of stock prices
F1 is function 1 of price
F2 is function 2 of price
C1 is search condition threshold 1
C2 is search condition threshold 2
...
i=1; %starting point
%start the search for the first condition
while F1(p(i))< C1
i=i+1;
end
%start the further search for the second condition
while F2(p(i))< C2
i=i+1;
end
...
so if input has more search conditions, there will be more loops.
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2011년 5월 5일
Definitely doable without loops.
Daniel Shub
Daniel Shub 2011년 5월 5일
Are you just trying to get to the final "i"? It doesn't even look like you need while loops. You should be able to loop over (or probably even nest) a bunch of "find" commands.

댓글을 달려면 로그인하십시오.


Daniel Shub
Daniel Shub 2011년 5월 5일
function looper(FcnHandle, NDIM, LoopIndices, Array)
LoopNo = length(Array)+1;
if LoopNo == NDIM
for ii = LoopIndices{LoopNo}
Array{LoopNo} = ii;
FcnHandle(Array)
end
else
for ii = LoopIndices{LoopNo}
Array{LoopNo} = ii;
looper(FcnHandle, NDIM, LoopIndices, Array)
end
end
end
looper(@(x)(disp(num2str([x{:}]))), 3, {1:3, 1:3, 1:3}, {})
  댓글 수: 1
Daniel Shub
Daniel Shub 2011년 5월 5일
Opps, this doesn't answer the revised question. I think it does what Cyclist did without eval or knowing the maximum number of loops.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Monte Carlo Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by