Your Account  •  Become a Member  •  Help  •  Search
 Forums ›› Technically Speaking ›› Programming ›› Vectorizing Nested FOR Loops

 wfeagin3   Registered: Jul 2012 Posts: 7 09-10-12 01:24 AM I am not a very experienced programmer, so I was hoping to get some useful feedback from this forum. I am coding in matlab and can code to accomplish what I need to do. It probably is not the most efficient coding, but I thought I would give it a shot to see what I could do. I am trying to create a backtesting program in Matlab where I am doing a parameter sweep of 8 variables for a function by using a set of 8 nested for-loops. It works fine. But the nested for-loops is a bottle-neck so it takes a very long time for the program to generate results. And it is only using 1 processor when I use nested for-loops. I believe that I can use all processors if I vectorize my code and get rid of the for-loops entirely which would increase the run time drastically. However, I've tried to vectorize it myself with no luck. Here is the basic layout of the nested for-loops. for a = 1:5; for b = 1:6; for c = 1:7; for d = 1:8; for e = 1:9; for f = 1:10; for g = 1:11; for h = 1:12; [value(:,a,b,c,d,e,f,g,h] = function(input,input1(a),input2(b),input3(c),input4(d),input5(e),input6(f),input7(g),input8(h)); end end end end end end end end Any help would be greatly appreciated! Thanks in advance! Edit/Delete • Quote • Complain
 NYDreamer   Registered: Feb 2010 Posts: 42 09-10-12 08:51 AM You should be making an 8-dimensional matrix (that's what vectorization means, building vectors and matrices) and apply your function to that. This operation will be a 1000 times faster that way. http://www.mathworks.com/help/matla....html#br8fs0d-1 Edit/Delete • Quote • Complain
 wfeagin3   Registered: Jul 2012 Posts: 7 09-10-12 10:44 PM Quote from NYDreamer: You should be making an 8-dimensional matrix (that's what vectorization means, building vectors and matrices) and apply your function to that. This operation will be a 1000 times faster that way. http://www.mathworks.com/help/matla....html#br8fs0d-1 Thanks for the reply! I've seen all the matlab examples and have done a thorough search online of vectorization for nested for-loops. However, I haven't been able to figure out the code need to vectorize 8 for-loops. I have visited the link above and seen it before, but I still can't figure it out. My programming experience is limited so I am not an experienced programmer in the slightest. I need a straight forward way that is dumbed down for my level of understanding and programming. Thanks! Edit/Delete • Quote • Complain