1

I'v a nested for loop but it slows a little bit processing, how can I make the nested loops an efficient. What I need is for every value of outer loop, the inner loop continues all its iterations. However, I don't think so it affects the computations as so much the two nested loops. My second question is, is parfor loop may affect the speed or may support my phenomena?

My Code:

n=2;
for i=1:500
for jj=1:n
    A{1}=['Obj' num2str(1)];
    A{2}=['Obj' num2str(2)];
end  
end
  • possible duplicate of [How to nest multiple parfor loops](http://stackoverflow.com/questions/20295579/how-to-nest-multiple-parfor-loops) – Daniel Apr 25 '14 at 18:08

1 Answers1

0

Your nested loops as they are do not exhibit any dependence on the loop parameters so you might want to reformat your code above. But in general, so long as your iterations are not dependent on each other and the number of iterations is large enough to support the delay in initializing the parallel processing, the parfor loop is allowed and it performs better (respectively). If your are working with a cell or matrix of some kind and your nested loops cover a connected portion of it, you can always use the linear index approach. For example

n=100;
s=0;
a=randi([1 n],n);
for i=1:n
   for j=1:n
       s=s+a(i,j);
   end
end

can be rewritten as

n=100;
s=0;
a=randi([1 n],n);
for i=1:n^2
    s=s+a(i);
end

But again if your array is large and your iterations do not depend on each other, it's usually better to take advantage of the 'parfor' construct.

oligilo
  • 161
  • 8
  • my inner loop is dependent on outer loop, as for every iteration of outter loop, inner loop should fully complete its iterations. Also I'v cell array implementation, so `parfor` loop didn't allow cell arrays. –  Apr 24 '14 at 22:41
  • by dependence of one iteration on the other I mean something like a(i,j)=a(j,i) rather than s=s+a(i,j); something that changes the array that you're referring to from one iteration to the next. If this is the case then Yes, parfor is not going to work for you. I'd suggest you take a look at Matlab's documentation on 'parfor' for a better explanation than what I'm trying to convey. – oligilo Apr 24 '14 at 22:46
  • yeah I understand this, but the problem is which is major problem I'm using cell arrays but how can I use `parfor` in that case? –  Apr 24 '14 at 22:49
  • You can't use parfor but you might still be able to use the [linear index](http://www.mathworks.com/help/matlab/math/matrix-indexing.html#f1-85511) approach. Also if your cell array is numeric (or if you can make it numeric and later convert back), you can use cell2mat together with a parfor. – oligilo Apr 24 '14 at 22:52
  • OK, `parfor` can be used with matrices. Please tell me `M = ['Obj' num2str(i:1:n)];` how can this repeat itself with `1` to `n` –  Apr 24 '14 at 22:55
  • It takes a little bit of preparation to turn a cell of strings into a matrix (if possible), and it's only worth it if your cell size is large enough and the operation after 'for' or 'parfor' is reversible. Given that you're referring to the cell M={'Obj' num2str(1:n)}, you can represent it as a matrix [0 1:n], (zero for 'Obj'), read something from this matrix in a parfor and then convert the result of your loop back into strings by replacing zero with 'Obj' etc. – oligilo Apr 24 '14 at 23:19
  • And again your (nested) loop is not doing anything on the variable A past the first iteration; 'you might want to reconsider your code above'. – oligilo Apr 24 '14 at 23:21
  • actually It has the array, which changes, all I want is efficient managment of nested loops, as the outter loop may be for infinte time but the inner loop is dependent on that. I did that, worrking fine but slows the speed. –  Apr 24 '14 at 23:25
  • I guess I'm out of ideas/guesses then :-) – oligilo Apr 24 '14 at 23:28