how to create an array of all permutations

Illustration
sophiawilson61 - 2020-07-03T09:42:19+00:00
Question: how to create an array of all permutations

I have a question about how to create the following array in a simple, concise way (for any permutations). Think of a 4 digit lock, but numbers allowed are not 0-9, but 1-6 or 0-17 (or any other range). For 0-9 there are 10^4 permutations, which should be a 10000 x 4 array, each row showing one of the permutations.   I thought of building the array from 4 vectors (thousand, hundred, tens, ones), but that is also cumbersome when looking at a 7digit lock with 11 possible numbers for each digit. example: 4 digits, possible numbers for each digit 1 - 6 (1296 permutations) unity_ones = [1;6]; which means that the ones vector can be created by:   n = 1296/6; ones_vector = []; for i=1:n ones_vector = [ones_vector,ones]; end   Then one can do the same for the tens_vector, hundreds_vector and thousands_vector... This seems not very efficient. I am sure there is an easier, one or two liner out there...

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

You can generate all permutations efficiently using MATLAB's ndgrid function (or meshgrid) combined with reshape. This allows you to generate the Cartesian product of all the possible digits for each place value (like the ones, tens, hundreds, etc.) in a more concise way.

Example for a 4-digit lock with possible digits from 1 to 6:

 

% Define the possible digits (1 to 6)
digits = 1:6;

% Create a grid for all positions (4-digit lock)
[ones, tens, hundreds, thousands] = ndgrid(digits, digits, digits, digits);

% Reshape to get the permutations as a matrix
permutations = [ones(:), tens(:), hundreds(:), thousands(:)];

% Display the result
disp(permutations);

Explanation:

  1. ndgrid(digits, digits, digits, digits):

    • This creates 4 grids, one for each digit position (ones, tens, hundreds, thousands).
    • Each grid is of size [6, 6, 6, 6] (because there are 6 possible digits for each position), but ndgrid ensures that each grid contains the correct values for each digit position.
  2. Reshaping:

    • ones(:), tens(:), hundreds(:), and thousands(:) convert the 4D grid arrays into column vectors.
    • The final result is a matrix where each row is a unique combination (or permutation) of the digits.

Output:

For the 4-digit lock with digits 1 to 6, the result is a 1296 x 4 matrix containing all 1296 possible permutations.

Generalizing:

For a d-digit lock with m possible values for each digit, you can generalize the approach by creating a d-dimensional grid:

 

% Define the possible digits (1 to m)
digits = 1:m;

% Create a grid for d positions
grid = ndgrid(repmat({digits}, 1, d));  % Create d-dimensional grid

% Reshape the grid into a matrix of permutations
permutations = reshape(cat(d, grid{:}), [], d);

% Display the result
disp(permutations);

Where m is the number of possible digits for each position, and d is the number of positions (digits). This approach is concise and works for any size lock and range of digits.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!