0

Suppose one has functions f(i) and g(j). How could one write a function h(k), where the domain k of h is made up of all k=i+j (i.e., each point h(k) is some function of f at i and g at j for all pairs of i and j satisfying k=i+j). For instance:

enter image description here

for all k=i+j. The domain of h would thus be k=2:25 and, for instance, h(3) would be equal to f(1)*g(2) + f(2)*g(1) since both of these combinations satisfy k=i+j.

This is simple to do using loops, but I wish to compose the function in anonymous function form (i.e., h = @(k) f(i) ... g(j)). How can this be accomplished?

1
  • According to your formal example (with the sigma sums), h is constant. Want you actually want is an algorithm to find all valid sets of i,j for a given k, such that k=i+j. Sounds like a math problem.
    – jiggunjer
    Oct 17, 2016 at 2:45

1 Answer 1

1

Let a and b be known variables for the domains i and j. Then the function you describe might look like this:

fun=@(k) sum(sum(transpose(f1(k-b(ismember(b,(k-a)))))*f2(b(ismember(b,(k-a))))))

where f1 and f2 are anonymous functions corresponding to f(i) and g(j). k is a valid scalar.

Note: it might not be considered good practice to use an anonymous function for something non-trivial.

Note2: I haven't considered scenarios with non-unique domains for i and j, nor negative values.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .