Recursive loops in MATLAB
2 views (last 30 days)
Show older comments
Ricardo Prada
on 2 Mar 2017
Commented: David Goodmanson
on 3 Mar 2017
I have a question regarding recursive loops in MATLAB.
Suppose I have a variable called "dimension" with value 3. The script that I need to run has the following structure:
dimension=3;
order=5;
for i1=1:order
for i2=1:order
for i3=1:order
if i1+i2+i3<=order
disp([i1,i2,i3])
end
end
end
end
That is, if "dimension=3", 3 nested loops will be required to run this script.
If "dimension=4", then the script shown above becomes:
dimension=4;
order=5;
for i1=1:order
for i2=1:order
for i3=1:order
for i4=1:order
if i1+i2+i3+i4<=order
disp([i1,i2,i3,i4])
end
end
end
end
end
That is, 4 nested loops in this case. And so on.
To solve this problem, I tried the following first (without the if condition i1+i2...<=order shown above, because I didn't know how to incorporate it into the script):
function result=recursiveLoop(dimension,order,level,iter,counter,expression)
if nargin==2 % initial call
level=1;
iter=zeros(dimension,1);
counter=0;
expression=zeros(order^dimension,dimension);
end
for i=1:order
iter(level)=i;
counter=counter+1;
expression(counter,:)=iter;
if level==dimension && i==order
result=expression;
end
if level~=dimension
result=recursiveLoop(dimension,order,level+1,iter,counter,expression);
end
end
end
But the output is not the one that one would expect from the above definition. Any ideas about how to solve this problem using, say, a recursive function? Is there a better way to solve this problem?
Thank you,
Ricardo
2 Comments
David Goodmanson
on 3 Mar 2017
Hi Ricardo, do you care what order the results are displayed in, i.e. right now the last index changes most quickly and the first index changes least quickly.
Accepted Answer
David Goodmanson
on 3 Mar 2017
Edited: David Goodmanson
on 3 Mar 2017
Hi Ricardo, here is a method using a function I created recently that is related to the adjustable-dimension issue. This is the first method that came to mind and other contributors here may well come up with more compact code. This creates a matrix whose rows are what you display in the loop.
dim = 3; ord = 5;
a = ord*(ones(1,dim));
z = ind2subb(a,1:ord^dim);
ind = (sum(z,2)>ord);
z(ind,:) = []; % knock out rows that don't meet the condition
% -----------------------------
function subs = ind2subb(siz,ind)
% matrix of sub indices from a linear index, for an array
% of size siz = [s1,s2 ...].
% each row in subs gives an index for each dimension of siz.
% same as ind2sub except subs is a matrix rather than a set of vectors.
%
% subs = ind2subb(siz,ind)
ndims = length(siz);
subs = zeros(length(ind),ndims)
ind = ind-1;
for j = 1:ndims
r = rem(ind,siz(j))
subs(:,j) = r;
ind = (ind -r)/siz(j);
end
subs = subs+1;
3 Comments
David Goodmanson
on 3 Mar 2017
Hi Ricardo, you're welcome. Looking at this again I think better coding in the last two script lines would be
ind = (sum(z,2)<=ord);
z = z(ind,:); % keep rows that meet the condition
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!