Function changes its behavior at each iteration of a for loop
이전 댓글 표시
Hi everyone, the problem is as follows, i want in a for loop change the behavior of a function at each iteration. Suppose the following for loop F or k = 1:4 B = Min_or_max([k 2 4]); end Let’s assume I have 2 functions called Compute_min and Compute_max computing respectively the maximum and the minimum value of a given vector/array taken as input parameter. I want that at the first iteration of the for loop compute_min is used instead of min_or_max and at the second iteration of the loop the function compute_max is used and so on.. Note that the for loop is placed in a function which takes as input a parameter, which should specify which function of (compute_min and compute_max ) should be used at which step. Example : for a = compute_min and b = compute_max the input parameter of the function in which the for loop is located can take the form [a b a a ] ; which means compute_min is used at the first iteration , compute_max is used at the second iteration , then compute_min at the third and again compute_min at the fourth iteration.
Many Thanks guys
채택된 답변
추가 답변 (1개)
wil
2015년 3월 23일
We'll call your input parameter list 'param', you can do something like the following:
for k= 1:4
switch param(k)
case 'a'
B = Compute_min([k 2 4]);
case 'b'
B = Compute_max([k 2 4]);
otherwise
% handle invalid param
end
end
Which checks the index of the parameters and calls the corresponding function depending on the value.
Wil
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!