请各位高手帮帮忙 用matlab 编写个程序 将-2,-1,0,1,2这五个数的120种排列组合全部显示出来?

如题所述

给你贴个函数原型吧,
function P = perms(V)
%PERMS All possible permutations.
% PERMS(1:N), or PERMS(V) where V is a vector of length N, creates a
% matrix with N! rows and N columns containing all possible
% permutations of the N elements.
%
% This function is only practical for situations where N is less
% than about 10 (for N=11, the output takes over 3 giga-bytes).
%
% Class support for input V:
% float: double, single
%
% See also NCHOOSEK, RANDPERM, PERMUTE.

% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 1.12.4.1 $ $Date: 2004/07/05 17:02:07 $

V = V(:).'; % Make sure V is a row vector
n = length(V);
if n <= 1, P = V; return; end

q = perms(1:n-1); % recursive calls
m = size(q,1);
P = zeros(n*m,n);
P(1:m,:) = [n * ones(m,1) q];

for i = n-1:-1:1,
t = q;
t(t == i) = n;
P((n-i)*m+1:(n-i+1)*m,:) = [i*ones(m,1) t]; % assign the next m
% rows in P.
end

P = V(P);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-16
perms([-2 -1,0,1,2])
第2个回答  2010-09-16
perms([-2 -1 0 1 2])
相似回答