I have a 1-dimensional cell array Z. Each cell of Z contains a vector. For example:
Z{1} = [1 2];
Z{2} = [3 4 5];
Z{3} = [6];
...
Z{length(Z)} = [10 11 12 13];
The sizes of those vectors are all different. What I want to do is to compare the sum of functions values of all possible combinations with one element from each Z{i}. That is I want to compare all the following combinations:
func(1) + func(3) + func(6) + ...
func(1) + func(4) + func(6) + ...
func(1) + func(5) + func(6) + ...
func(2) + func(3) + func(6) + ...
func(2) + func(4) + func(6) + ...
func(2) + func(5) + func(6) + ...
...
...
and I want to know which combination yields the maximum.
How can I smartly do this? The smarter, the better. But I am also looking for any working code. The problem size will be small.
Note: The actual values used in this example, 1, 2, 3, 4, 5, 6, … are just examples. They don’t have any specific pattern.
Consider the following solution, it has a cycle but it does what you want linearly in time instead of exponentially.
Iteratively, the algorithm runs throughout all the rows of
Zmaking all the possible paths among the entries of the rowZ{i}. Nonetheless, each entry is parsed just once, thus you save complexity.test with squares
for instance
46 = 1^2 + 3^2 + 6^2,49 = 2^2 + 3^2 + 6^2…So far I am not sure you can avoid cycles completely. What I do here is dynamically constructing the solution adding one element of your cell at every iteration.
Tensor summation technique (
t = bsxfun(@plus,res,arrayfun(f,(Z{i}))')) from this answer.