"Samuel Whiteley" <sam.whiteley@gmail.com> wrote in message <k558vo$ld4$1@newscl01ah.mathworks.com>...
> I want to find all row permutations of the vectors 1:512, 1:512, and 1:180 in a single matrix, call it A. A would be size Nx3, where N is 512*512*180.
>
> Some examples of permutations starting with the first and ending with the last:
> A=
> [1 1 1;
> 2 1 1;
> 3 1 1;
> ...
> 512 1 1;
> 1 2 1;
> 2 2 1;
> ...
> 512 512 180];
>
> I read perms, but that only takes in one row vector.
> A triple nested forloop to set the values is much slower than any vectorization. Code that does work but I would rather have work in under 3 seconds (part of an algorithm):
>
> A = zeros(512*512*180,3);%preallocate
> count=0;%variable to step through row by row
> for k=1:180
> for j=1:512
> for i=1:512
> count=count+1;
> A(count,1:3) = [i,j,k];
> end
> end
> end
>
>
> Any help for an efficient alternative to my nested loops is appreciated. Matlab deals with matrices, multiplication, and vectorization much faster than loops.
>
> Thanks,
> Sam
I had this lying around... i think it does what you want: (watch for line wraps)
levels=[512 512 180];
replication=cumprod(levels)./levels;
points=ones(prod(levels),length(levels));
for c=1:length(levels)
thislevels=1:levels(c);
thislevels=repmat(thislevels,
replication(c),1,prod(levels)./(replication(c)*numel(thislevels))]);
points(:,c)=thislevels(:);
end
