任何人都可以帮助编写代码来生成一个矩阵,其中每行应包含一个新的排列(排列不重复)?
我尝试了以下内容:
%Generate an array of 8 elements randomly
array=floor(randi([0,100],1,K));
%Generate all permutations of 'array'. Each row signifies a new arrangement
%all_matrix=perms(array);
%// Create all possible permutations (with repetition) of letters stored in x
C = cell(K, 1); %// Preallocate a cell array
[C{:}] = ndgrid(array); %// Create K grids of values
y = cellfun(@(array){array(:)}, C); %// Convert grids to column vectors
all_matrix_repetition = [y{:}];
all_matrix=zeros(factorial(K),K);
for i=1:size(all_matrix_repetition,1)
if length(all_matrix_repetition(i,:))==length(unique(all_matrix_repetition(i,:)))
all_matrix(count,:)=all_matrix_repetition(i,:);
count=count+1;
end
end
但是得到了内存错误。我正在使用Matlab 2011a。请帮帮我。
答案 0 :(得分:0)
如果我理解正确,我认为这样做:
arraySize = 3;
array = randi(100,[1 arraySize]);
indexes = perms(1:arraySize);
permutated = array(indexes);
你会得到的地方,例如:
array =
19 24 42
indexes =
3 2 1
3 1 2
2 3 1
2 1 3
1 2 3
1 3 2
permutated =
42 24 19
42 19 24
24 42 19
24 19 42
19 24 42
19 42 24