Recursive loops in MATLAB

2 次查看(过去 30 天)
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 个评论
David Goodmanson
David Goodmanson 2017-3-3
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.
Ricardo Prada
Ricardo Prada 2017-3-3
Hi David. I don't. I just want to execute some code inside the if condition. Thanks for your prompt reply.

请先登录,再进行评论。

采纳的回答

David Goodmanson
David Goodmanson 2017-3-3
编辑:David Goodmanson 2017-3-3
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 个评论
Ricardo Prada
Ricardo Prada 2017-3-3
Hi David,
Never mind. Your approach will solve my actual problem too. What I need to do now is to use the resulting "z" matrix, and put it in another loop.
Thanks a lot for your time and help. I greatly appreciate it.
Best,
Ricardo
David Goodmanson
David Goodmanson 2017-3-3
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by