Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:25:32+00:00 2026-06-14T11:25:32+00:00

I have a 1-dimensional cell array Z. Each cell of Z contains a vector.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:25:33+00:00Added an answer on June 14, 2026 at 11:25 am

    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 Z making all the possible paths among the entries of the row Z{i}. Nonetheless, each entry is parsed just once, thus you save complexity.

     N = 3;
    
     Z = cell(1,N);
    
     Z{1} = [1 2];
     Z{2} = [3 4 5];
     Z{3} = [6];
    
     f = @(x) x.^2;  %% Test function
    
    
    
    disp('init')
    res = arrayfun(f,(Z{1}))     %% Init step. Image of Z{1}
    for i = 2 : N
       disp(i)      %% just to have an idea of where you are in the process
       disp(res)
    
       t = bsxfun(@plus,res,arrayfun(f,(Z{i}))')  %In a tensor way you build all
                                                  %the possible sum of the res and f(Z{i})
                                                  %making all paths.
       res = reshape(t,1,[])                      %You put the tensor sum on a single
                                                  %row to be able to iterate.  
       disp('END step-------')
    end
    

    test with squares

    res =
    
    46    53    62    49    56    65
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2-dimensional array of objects and I basically want to databind each
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have an multi-dimensional array that I want to send to a PHP script
I have a cell array C containing 380 matrices and want to put them
I have a three dimensional ndarray of 2D coordinates, for example: [[[1704 1240] [1745
I have following 2 dimensional array input: var arr = [ ['A', ['Sun','Moon']], ['B',
I have a multi-dimensional array, no problem. How do I interrogator one of the
I have an 2 dimensional array. I write 3 values into the array during
I have a multi dimensional array. The only actual values (other than other arrays)
I have a two-dimensional array. When I print/dump this I get the following My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.