Most efficient way to generate combinations

Dear all; I need to generate a large serie of combinations and it is taking a lot. I'm using 3 cycles one inside the other but I'm sure there's a better way to achieve this.
Here's what I'm currently doing:
indices=0; %indices matrix
l=1;
for i=1:200;
for j=1:300;
for k=1:250;
indices(l,1)=i-1;
indices(l,2)=j-1;
indices(l,3)=k-1;
l=l+1;
end
end
end
Please note that i, j and k have different lengths (thus values).
Any help would be greatly appreciated!
Thank you!

Antworten (3)

Walter Roberson
Walter Roberson am 29 Apr. 2013

1 Stimme

[I, J, K] = ndgrid(0:199, 0:299, 0:249);
indices = horzcat(I(:), J(:), K(:));
Jonathan Sullivan
Jonathan Sullivan am 29 Apr. 2013

0 Stimmen

You can use allcomb (which can be found here)
a = (1:200)-1;
b = (1:300)-1;
c = (1:250)-1;
indices = allcomb(a,b,c);
Lorenzo
Lorenzo am 30 Apr. 2013
Bearbeitet: Lorenzo am 30 Apr. 2013

0 Stimmen

Thank you both!
Eventually I did it with
[p,q,n] = meshgrid(1:length(x),1:length(y),1:length(z));
indices=[p(:)-1 q(:)-1 n(:)-1];

Kategorien

Mehr zu Language Fundamentals finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 29 Apr. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by