Alternative to for/while cycle

Asked by Luca Freilino

Luca Freilino (view profile)

on 10 Apr 2019
Latest activity Commented on by Stephen Cobeldick

Stephen Cobeldick (view profile)

on 10 Apr 2019
Accepted Answer by Walter Roberson

Walter Roberson (view profile)

Hi, I have a question. I'm working at my master thesis and I'm writing a code quite computationally heavy. I'd like to light it and a possible option should be the replacement of the for and while cycles with another option. Do you have idea about any possible replacement?
This is a portion of the code (consider that the table_percentage matrix is already pre-allocated):
for a = ceil((minimum/single_step)+1):(length(steps)-((number_surrogates-1)*(minimum/single_step)))
for b = ceil((minimum/single_step)+1):(length(steps)-((number_surrogates-1)*(minimum/single_step)))
for c = ceil((minimum/single_step)+1):(length(steps)-((number_surrogates-1)*(minimum/single_step)))
for d = ceil((minimum/single_step)+1):(length(steps)-((number_surrogates-1)*(minimum/single_step)))
if steps(a)+steps(b)+steps(c)+steps(d)==1
table_percentage(index,:) = [steps(a), steps(b), steps(c), steps(d)];
index = index+1;
end
end
end
end
end
Thank you very much for every help.
Luca

Release

R2018b

Answer by Walter Roberson

Walter Roberson (view profile)

on 10 Apr 2019

v = steps(ceil((minimum/single_step)+1):(length(steps)-((number_surrogates-1)*(minimum/single_step))));
[A,B,C,D] = ndgrid(v);
mask = A+B+C+D == 1;

Luca Freilino

Luca Freilino (view profile)

on 10 Apr 2019
Thank you very much! It works. I apologize if I take advantage of your politeness, but I have another question.
my nested loops vary on different range:
for example the first three vary from 0.05 to 0.60, the fourth and the fifth from 0.05 to 0.15 and the last one from 0.15 to 0.25. Is there a way to use the ndgrid function on vector of different length? I have to respect these limits ([0.60 0.70], [0.15 0.20], [0.15 0.25]), where the first number is the minimum of their sum and the second one is the maximum.
I don't know if I succeded in explaining, it's a quite twisted reasoning haha.
By the way, thank you again :)
Stephen Cobeldick

Stephen Cobeldick (view profile)

on 10 Apr 2019
"Is there a way to use the ndgrid function on vector of different length?"
Simply read the ndgrid documentation and provide each of those vectors as an input argument (in the appropriate order, of course).

Answer by Stephen Cobeldick

Stephen Cobeldick (view profile)

on 10 Apr 2019
Edited by Stephen Cobeldick

Stephen Cobeldick (view profile)

on 10 Apr 2019

"a possible option should be the replacement of the for and while cycles with another option"
Why do you think that the loops themselves are the slow part? Have you profiled your code?
A quick look at your code shows that you could easily move the loop iteration vector definition before the loops, as it is indetical for all loops (currently you are pointlessly redefining exactly the same vector multiple times):
V = ceil((minimum/single_step)+1):(length(steps)-((number_surrogates-1)*(minimum/single_step)))
for a = V
for b = V
for c = V
for d = V
if steps(a)+steps(b)+steps(c)+steps(d)==1
table_percentage(index,:) = [steps(a), steps(b), steps(c), steps(d)];
index = index+1;
end
end
end
end
end
Another option would be to avoid the loops entirely using ndgrid to generate the indices:
[A,B,C,D] = ndgrid(steps(V));
X = [A(:),B(:),C(:),D(:)];
table_percentage = X(sum(X,2)==1,:);
Note that this method will only work for V with a small number of elements, and you will need to adjust the indexing to suit steps and table_percentage (which you have told us nothing about).