Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Find all row permutations of a matrix

Subject: Find all row permutations of a matrix

From: Samuel Whiteley

Date: 11 Oct, 2012 01:56:08

Message: 1 of 4

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 for-loop 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

Subject: Find all row permutations of a matrix

From: matt dash

Date: 11 Oct, 2012 02:05:08

Message: 2 of 4

"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 for-loop 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

Subject: Find all row permutations of a matrix

From: Roger Stafford

Date: 11 Oct, 2012 02:23:07

Message: 3 of 4

"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.
- - - - - - - - - -
  These aren't permutations, so 'perms' will do you no good. Just use 'ndgrid':

 [I,J,K] = ndgrid(1:512,1:512,1:180);
 A = [I(:),J(:),K(:)];

Roger Stafford

Subject: Find all row permutations of a matrix

From: Samuel Whiteley

Date: 11 Oct, 2012 02:56:08

Message: 4 of 4

"Roger Stafford" wrote in message <k55aib$qd2$1@newscl01ah.mathworks.com>...
> "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.
> - - - - - - - - - -
> These aren't permutations, so 'perms' will do you no good. Just use 'ndgrid':
>
> [I,J,K] = ndgrid(1:512,1:512,1:180);
> A = [I(:),J(:),K(:)];
>
> Roger Stafford


Thank you Roger! That is exactly what I was looking for.

Sorry about the misnomers. It's clear to me now after writing the thread that these aren't permutations, but I'm glad that the rest of it was interpreted correctly.

Sam

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us