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

 採用された回答

Stephen23
Stephen23 2015 年 3 月 23 日
編集済み: Stephen23 2015 年 3 月 23 日

1 投票

If you have two (or more) pre-defined functions that you wish to select between in a loop, then you could consider using function handles to select and pass the functions. Something like this:
fun = {@min,@max};
idx = [1,2,1,1];
mat = [1,2,3,4;5,6,7,8;9,10,11,12];
out = nan(size(idx));
for k = 1:numel(idx)
out(k) = fun{idx(k)}(mat(:,k));
end
where
>> mat
mat = 1 2 3 4
5 6 7 8
9 10 11 12
>> out
out = 1 10 3 4
shows that it has taken the minimum of columns 1, 3, and 4, and the maximum of column 2, exactly as the vector idx selected. How it works:
  • fun is a cell array of the function handles that you want to call.
  • idx is a vector of the indices of the functions in fun to call on each loop.
  • mat is some fake numeric data, with as many columns as idx has elements.
  • out is the min/max value of each column of mat, depending on which function was selected for that column by the corresponding index in idx.

3 件のコメント

kerin
kerin 2015 年 3 月 23 日
Many many many thanks stephen, this is exactly what i was looking for.. Do you have an idea what i can do if the number of input parameter of my two funcion are diferent ? For example
fun = {@rectangle_area,@square_area};
idx = [1,2,1,1]; length = 2 ; width = 3 ; raduis = 5 ;
a= rectangle_area(length, width) % a = 2*(length+width)
b= square_area(raduis) % b = pi*raduis^2
for k = 1:numel(idx)
out(k) = fun{idx(k)}(????);
end
what should come at ???? place ?
a lot of thanks again
Stephen23
Stephen23 2015 年 3 月 24 日
編集済み: Stephen23 2015 年 3 月 24 日
Ah, changing the requirements after the original question has been answered... Oh well :) If the functions require a different number of arguments, then this can be achieved by placing the arguments in a cell array too:
fun = {@rectangle_area,@square_area};
dat = {{2,3},{5}}; % {{length,width},{radius}}
idx = [1,2,1,1];
out = nan(size(idx));
for k = 1:numel(idx)
x = idx(k);
out(k) = fun{x}(dat{x});
end
This assumes that the length/width/radius values are the same for all loop iterations. If they are different, then define one element of dat for each iteration:
dat = {{2,3},{5},{7,6},{3,6}};
and call it like so:
out(k) = fun{x}(dat{k});
Alternatively you could use a switch statement, as per wil's answer, in which case the inputs can be hard-coded.
Note that you should never call a variable length, size, i, j, cd, table, ans, etc, as these are all inbuilt variables or functions. You can use which to identify inbuilt functions and variables.
kerin
kerin 2015 年 3 月 24 日
ohh yessssssssss !!!!!! u're amazing !!! Thanks.. be blessed !!

サインインしてコメントする。

その他の回答 (1 件)

wil
wil 2015 年 3 月 23 日

0 投票

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 についてさらに検索

タグ

質問済み:

2015 年 3 月 23 日

コメント済み:

2015 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by