MATLAB Answers

Making different combinations from tree branches in matlab

6 views (last 30 days)
Hello, I have to make all possible combinations from a tree, starting from parent node and ends at the last leaf nodes. I made a code for a fix number of tree branches (4 APs candidate locations),and it worked well for me but i need to make it dynamic so it works for different number of APs depending upon the scenario... Here the dimensions of AP1=[x,y] always, C{1} will be something like {a,b;c,d; e,f;g,h;}, where the length is "j" and from C{2} onward the matrix will be multidimensional... so that's where i need to code efficiently so i can get the desired APconfig.
s=1;
for i=1: length(C{1})
for j=1: length(C{2})
for k=1:length(C{2}{j})
for l=1: length(C{3})
for m=1:length(C{3}{l})
APconfig(s,:)=[{Ap1} {C{1}(i,:)} {C{2}{j}(k,:)} {C{3}{l}(m,:)}];
s=s+1;
end
end
end
end
end

Accepted Answer

Sohaib  Bin Altaf
Sohaib Bin Altaf on 5 Jul 2018
I tried and this code worked for me.
for i=1:length(C{1})
combination(i,:)= [{Ap1} {C{1}(i,:)}]
end
i=1
for x=2:length(C)
if x==2
;
else
combination=comb2
comb2={}
i=1 %adjust
end
for y=1:length(C{x})
for z=1:length(C{x}{y})
comb2(i,:) = [combination{ y ,:} {C{x}{y}(z,:)}];
i=i+1;
end end end

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!