min*_*adi 8 matlab permutation cartesian-product
假设我有4个字母,我想在3个地方安排它们(允许重复),所以我会有4 3 = 64个可能的排列.我该如何计算和打印它们?
简化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应存储您所追求的排列.