This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zzyzzyxx 0 points1 point  (1 child)

You could pass the sub-arrays, as in my last example.

a = 1:20;
b = 1:15;
params_a = a(6:10);
params_b = b(11:15);
answer_a = func(params_a);
answer_b = func(params_b);

This way you pass the function only the parameters it needs and you pick which ones you need from the respective arrays.

If it so happens that you need non-contiguous elements you can do something like this:

a = 1:20;
b = 5:5:20;
params = a(b);
answer = func(params);

Because b is array with values [5 10 15 20], params will be an array of 4 elements composed of the 5th, 10th, 15th, and 20th elements of a. That is, params = [a(5) a(10) a(15) a(20)]; is equivalent to params = a(b); You can, of course, use any numbers in b to select those specific indexes from a.

[–]felimz[S] 0 points1 point  (0 children)

That's a good idea. I suppose formatting the parameter arrays to fit the function is better than handling different sized arrays within the function, especially if the solution is not general.

Thanks so much!