0
for i=1:numResults
    tempStructure=struct;
    for j=1:options.numDates
        stringJ=num2str(j);
        [tempStructure.(['temp' stringJ]),tempStructure.(['tempHHI' stringJ])]=fillTemp(resultsStructure.(['results' stringJ]),resultsStructure.(['resultsHHI' stringJ]),options.resultSize,i);
    end
end

Here, we can assume that resultStructure has fields (specified dynamically) before the loop and every field is present in resultStructure.

fillTemp is a complicated function that is very difficult to vectorize.

Is there any benefit in removing "for" loops for i and j by way of bsxfun in the code above to improve performance? Any alternative method for speeding up the above code is also appreciated.

Note: I understand the way I am defining and using structures with dynamic fields is not the most optimal solution but this is a working solution and I do not want to tinker with the working solution.

I am using MATLAB R2018a.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
lonstud
  • 525
  • 2
  • 19
  • 6
    What sort of benefits do you expect? Performance? Readability? Robustness? Vectorized code is typically shorter, so I suppose you could expect better readability. Other than that, the only way to know what benefits vectorization would yield is to do it and see what happened. If you're trying to determine whether it's worth spending time to vectorize _this particular piece of code_, the answer depends on how this specific section performs with respect to everything else. – Dev-iL Nov 26 '19 at 09:00
  • Thank you for your insight. I understand what you mean. Please put this as the answer so that I can close this question. – lonstud Nov 26 '19 at 16:50
  • 1
    If you want to speed up your code, your first priority should be to get rid of those dynamic field names. Use cell arrays to hold sets of matrices that you want to index using numbers. `structure.tempHHI{j}` is a lot simpler to read and a lot more efficient. Vectorizing this code is pointless, the cost of the loop is minimal compared to the cost of your indexing. – Cris Luengo Nov 27 '19 at 14:44

1 Answers1

1

It looks like you're asking the wrong question here, and here's why: it appears your objective is improving the performance of your code as a whole, but for some unspecified reason you've decided to focus on a specific part, and attempted to apply bsxfun to it. Based on this information it seems you're facing what's known as an XY problem.

It's good that you're aware of vectorization, but you should remember it is only one of many techniques useful for this purpose (in fact there's a whole book about it). Why, for example, did you not consider parfor1, or mex?

A well-known saying among programmers is "premature optimization is the root of all evil". Practically, this means that you should first determine the bottlenecks in your program, and only then think about how to address them. The first part can be done using the profiler (example and additional explanation here). The second part depends on the specifics of your code (which are not sufficiently detailed in your question, see also: How to create a Minimal, Reproducible Example), so a general answer cannot be given here unfortunately.

As mentioned in a previous comment, the benefits of vectorization could be improved performance, and in the likely case the code becomes shorter, better readability and reduced probability of bugs (i.e. longer codes tend to produce more bugs, for a constant "bugs per line-of-code").

P.S.
Since you mentioned you worked with R2018a, bsxfun is not even necessary anymore due to the introduction of implicit expansion.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99