Recursive loops in MATLAB

4 vues (au cours des 30 derniers jours)
Ricardo Prada
Ricardo Prada le 2 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 commentaires
David Goodmanson
David Goodmanson le 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.
Ricardo Prada
Ricardo Prada le 3 Mar 2017
Hi David. I don't. I just want to execute some code inside the if condition. Thanks for your prompt reply.

Connectez-vous pour commenter.

Réponse acceptée

David Goodmanson
David Goodmanson le 3 Mar 2017
Modifié(e) : David Goodmanson le 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 commentaires
Ricardo Prada
Ricardo Prada le 3 Mar 2017
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 le 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by