请教matla排列组合程序编写,都来看看啊
比如:A=; B=想得到:C=
我现在用的思路是:随机在A里取一个,B里取一个数字,如后组合到C中;如果C中已经有这个组合就删去。
这样的方法,缺点:多维时,慢;更多维是则不能得到。
哪位高手有更好的方法,思路,或已有程序。
或matlab中已有函数可以实现这个功能。
谢谢啦。 个人水平专业有限, 不太明了楼主的意思?:@) 我没有表达清楚。就是有n相同的个数列a1,a2。。。an。a1=; a2也是=
然后,我想得到一个矩阵B,矩阵B的第一列由a1中任意一个数构成;第二列由a2中任意一个数构成;以此类推。即,矩阵B是由a1。。。an中的数字任意组成的排列组合。B是一个n.^m行,n列的矩阵。例如: a1=; a2=; 想得到C=; 通俗说:就是a1里任意拿一数,a2里任意拿一数,看有多少种不同的排列组合。 doc nchoosek 回复 jiajuan911 的帖子
原来是重复排列的问题, 懒得自己写, 搜了下现成的
还没试, 请自行参考试试
http://www.mathworks.com/matlabcentral/fileexchange/7147-combn
http://www.ilovematlab.cn/viewthread.php?tid=72038&sid=QsgNox
http://www.aiseminar.cn/bbs/viewthread.php?tid=102
...
这不就是随机生成n行m列的整数介于1到3吗?
给你两个提示,你自己查查帮助文件randintrandsrc 刚才想错了,nchoosek组合下标索引似乎缺数,用repmat更简单:a=6:10;
b=11:15;
d=);b(sort(repmat(1:5,)))]' 厉害,谢谢大伙啦。randsrc厉害。 回复 qibbxxt 的帖子
这两个函数以前没用过, 刚看下帮助文件学习了, 谢谢
但怎麽求出全部的重复排列? 求不出重复的。只能得出所有非重复的排列组合。 回复 ChaChing 的帖子
这个我觉得需要循环,也许还有更好的办法,可能慢慢才会发现 回复 9 # ChaChing 的帖子
function = combn(V,N)
% COMBN - all combinations of elements
% M = COMBN(V,N) returns all combinations of N elements of the elements in
% vector V. M has the size (length(V).^N)-by-N.
%
% = COMBN(V,N) also returns the index matrix I so that M = V(I).
%
% V can be an array of numbers, cells or strings.
%
% Example:
% M = COMBN(,3) returns the 8-by-3 matrix:
% 0 0 0
% 0 0 1
% 0 1 0
% 0 1 1
% ...
% 1 1 1
%
% All elements in V are regarded as unique, so M = COMBN(,3) returns
% a 8-by-3 matrix with all elements equal to 2.
%
% NB Matrix sizes increases exponentially at rate (n^N)*N.
%
% See also PERMS, NCHOOSEK
% and ALLCOMB and PERMPOS on the File Exchange
% for Matlab R13, R14
% version 4.1 (jan 2010)
% (c) Jos van der Geest
% email: jos@jasen.nl
% History
% 1.1 updated help text
% 2.0 new faster algorithm
% 3.0 (aug 2006) implemented very fast algorithm
% 3.1 (may 2007) Improved algorithm Roger Stafford pointed out that for some values, the floor
% operation on floating points, according to the IEEE 754 standard, could return
% erroneous values. His excellent solution was to add (1/2) to the values
% of A.
% 3.2 (may 2007) changed help and error messages slightly
% 4.0 (may 2008) again a faster implementation, based on ALLCOMB, suggested on the
% newsgroup comp.soft-sys.matlab on May 7th 2008 by "Helper". It was
% pointed out that COMBN(V,N) equals ALLCOMB(V,V,V...) (V repeated N
% times), ALLCMOB being faster. Actually version 4 is an improvement
% over version 1 ...
% 4.1 (jan 2010) removed call to FLIPLR, using refered indexing N:-1:1
% (is faster, suggestion of Jan Simon, jan 2010), removed REPMAT, and
% let NDGRID handle this
error(nargchk(2,2,nargin)) ;
if isempty(V) || N == 0,
M = [] ;
IND = [] ;
elseif fix(N) ~= N || N < 1 || numel(N) ~= 1 ;
error('combn:negativeN','Second argument should be a positive integer') ;
elseif N==1,
M = V(:).' ;
IND = 1:numel(V) ;
else
% speed depends on the number of output arguments
if nargout<2,
M = local_allcomb(V,N) ;
else
% indices requested
IND = local_allcomb(1:numel(V),N) ;
M = V(IND) ;
end
end
% LOCAL FUNCTIONS
function Y = local_allcomb(X,N)
% See ALLCOMB, available on the File Exchange
if N>1
% create a list of all possible combinations of N elements
= ndgrid(X) ;
% concatenate into one matrix, reshape into 2D and flip columns
Y = reshape(cat(N+1,Y{:}),[],N) ;
else
% no combinations have to be made
Y = X(:) ;
end
% =========================================================================
% Previous algorithms
% Version 3.2
% % COMBN is very fast using a single matrix multiplication, without any
% explicit for-loops.
% nV = numel(V) ;
% % use a math trick
% A = +(1/2) ;
% B = ;
% IND = rem(floor((A(:) * B(:)')),nV) + 1 ;
% M = V(IND) ;
% Version 2.0
% for i = N:-1:1
% X = repmat(1:nV,nV^(N-i),nV^(i-1));
% IND(:,i) = X(:);
% end
% M = V(IND) ;
% Version 1.0
% nV = numel(V) ;
% % don waste space, if only one output is requested
% = ndgrid(1:nV) ;
% IND = fliplr(reshape(cat(ndims(IND{1}),IND{:}),[],N)) ;
% M = V(IND) ;
% Combinations using for-loops
% can be implemented in C or VB
% nv = length(V) ;
% C = zeros(nv^N,N) ; % declaration
% for ii=1:N,
% cc = 1 ;
% for jj=1:(nv^(ii-1)),
% for kk=1:nv,
% for mm=1:(nv^(N-ii)),
% C(cc,ii) = V(kk) ;
% cc = cc + 1 ;
% end
% end
% end
% end
下面的程序来自mathworks,可以实现这个功能
补充俩函数:
fullfact
ff2d 路过看下。。 学习学习!顺便帮忙顶下!{:{39}:}
页:
[1]
2