如何在MATLAB中找到所有排列(重复)?

min*_*adi 8 matlab permutation cartesian-product

假设我有4个字母,我想在3个地方安排它们(允许重复),所以我会有4 3 = 64个可能的排列.我该如何计算和打印它们?

Eit*_*n T 8

简化Amro的答案,您可以使用:

%// Sample data
x = 'ABCD';                 %// Set of possible letters
K = 3;                      %// Length of each permutation

%// Create all possible permutations (with repetition) of letters stored in x
C = cell(K, 1);             %// Preallocate a cell array
[C{:}] = ndgrid(x);         %// Create K grids of values
y = cellfun(@(x){x(:)}, C); %// Convert grids to column vectors
y = [y{:}];                 %// Obtain all permutations
Run Code Online (Sandbox Code Playgroud)

Matrix y应存储您所追求的排列.


小智 4

N_PERMUTE_K文件交换的功能怎么样?