声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 8423|回复: 16

[综合讨论] 请教matla排列组合程序编写,都来看看啊

  [复制链接]
发表于 2010-9-4 15:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
比如:A=[1;2;3];    B=[1;2;3]
想得到:C=[11; 12; 13; 21;22;23;31;32;33]
我现在用的思路是:随机在A里取一个,B里取一个数字,如后组合到C中;如果C中已经有这个组合就删去。
这样的方法,缺点:多维时,慢;更多维是则不能得到。
哪位高手有更好的方法,思路,或已有程序。
或matlab中已有函数可以实现这个功能。
谢谢啦。
回复
分享到:

使用道具 举报

发表于 2010-9-4 16:49 | 显示全部楼层
个人水平专业有限, 不太明了楼主的意思?:@)
 楼主| 发表于 2010-9-5 08:45 | 显示全部楼层
我没有表达清楚。就是有n相同的个数列a1,a2。。。an。a1=[1  2  ...m]; a2也是=[1 2 ...m]
然后,我想得到一个矩阵B,矩阵B的第一列由a1中任意一个数构成;第二列由a2中任意一个数构成;以此类推。即,矩阵B是由a1。。。an中的数字任意组成的排列组合。B是一个n.^m行,n列的矩阵。例如: a1=[1  2 3]; a2=[1 2 3];   想得到C=[1 1;1 2; 1 3;2 1;2 2;2 3;3 1;3 2;3 3]; 通俗说:就是a1里任意拿一数,a2里任意拿一数,看有多少种不同的排列组合。
发表于 2010-9-5 20:33 | 显示全部楼层
doc nchoosek
发表于 2010-9-5 20:43 | 显示全部楼层
发表于 2010-9-5 21:16 | 显示全部楼层
这不就是随机生成n行m列的整数介于1到3吗?
给你两个提示,你自己查查帮助文件
  1. randint
复制代码
  1. randsrc
复制代码

评分

1

查看全部评分

回复 支持 0 反对 1

使用道具 举报

发表于 2010-9-5 22:40 | 显示全部楼层
刚才想错了,nchoosek组合下标索引似乎缺数,用repmat更简单:
  1. a=6:10;
  2. b=11:15;
  3. d=[repmat(a,[1,5]);b(sort(repmat(1:5,[1 5])))]'
复制代码

评分

1

查看全部评分

 楼主| 发表于 2010-9-6 08:51 | 显示全部楼层
厉害,谢谢大伙啦。randsrc厉害。
发表于 2010-9-6 14:56 | 显示全部楼层
回复 qibbxxt 的帖子
这两个函数以前没用过, 刚看下帮助文件学习了, 谢谢
但怎麽求出全部的重复排列?
 楼主| 发表于 2010-9-8 09:24 | 显示全部楼层
求不出重复的。只能得出所有非重复的排列组合。
发表于 2010-9-8 17:41 | 显示全部楼层
回复 ChaChing 的帖子


    这个我觉得需要循环,也许还有更好的办法,可能慢慢才会发现

评分

1

查看全部评分

发表于 2011-4-1 09:17 | 显示全部楼层
回复 9 # ChaChing 的帖子
  1. function [M,IND] = combn(V,N)
  2. % COMBN - all combinations of elements
  3. %   M = COMBN(V,N) returns all combinations of N elements of the elements in
  4. %   vector V. M has the size (length(V).^N)-by-N.
  5. %
  6. %   [M,I] = COMBN(V,N) also returns the index matrix I so that M = V(I).
  7. %
  8. %   V can be an array of numbers, cells or strings.
  9. %
  10. %   Example:
  11. %     M = COMBN([0 1],3) returns the 8-by-3 matrix:
  12. %       0     0     0
  13. %       0     0     1
  14. %       0     1     0
  15. %       0     1     1
  16. %       ...
  17. %       1     1     1
  18. %
  19. %   All elements in V are regarded as unique, so M = COMBN([2 2],3) returns
  20. %   a 8-by-3 matrix with all elements equal to 2.
  21. %
  22. %   NB Matrix sizes increases exponentially at rate (n^N)*N.
  23. %
  24. %   See also PERMS, NCHOOSEK
  25. %        and ALLCOMB and PERMPOS on the File Exchange

  26. % for Matlab R13, R14
  27. % version 4.1 (jan 2010)
  28. % (c) Jos van der Geest
  29. % email: jos@jasen.nl

  30. % History
  31. % 1.1 updated help text
  32. % 2.0 new faster algorithm
  33. % 3.0 (aug 2006) implemented very fast algorithm
  34. % 3.1 (may 2007) Improved algorithm Roger Stafford pointed out that for some values, the floor
  35. % operation on floating points, according to the IEEE 754 standard, could return
  36. % erroneous values. His excellent solution was to add (1/2) to the values
  37. % of A.
  38. % 3.2 (may 2007) changed help and error messages slightly
  39. % 4.0 (may 2008) again a faster implementation, based on ALLCOMB, suggested on the
  40. %     newsgroup comp.soft-sys.matlab on May 7th 2008 by "Helper". It was
  41. %     pointed out that COMBN(V,N) equals ALLCOMB(V,V,V...) (V repeated N
  42. %     times), ALLCMOB being faster. Actually version 4 is an improvement
  43. %     over version 1 ...
  44. % 4.1 (jan 2010) removed call to FLIPLR, using refered indexing N:-1:1
  45. %     (is faster, suggestion of Jan Simon, jan 2010), removed REPMAT, and
  46. %     let NDGRID handle this

  47. error(nargchk(2,2,nargin)) ;

  48. if isempty(V) || N == 0,
  49.     M = [] ;
  50.     IND = [] ;
  51. elseif fix(N) ~= N || N < 1 || numel(N) ~= 1 ;
  52.     error('combn:negativeN','Second argument should be a positive integer') ;
  53. elseif N==1,
  54.     M = V(:).' ;
  55.     IND = 1:numel(V) ;
  56. else
  57.     % speed depends on the number of output arguments
  58.     if nargout<2,
  59.         M = local_allcomb(V,N) ;
  60.     else
  61.         % indices requested
  62.         IND = local_allcomb(1:numel(V),N) ;
  63.         M = V(IND) ;
  64.     end
  65. end

  66. % LOCAL FUNCTIONS

  67. function Y = local_allcomb(X,N)
  68. % See ALLCOMB, available on the File Exchange
  69. if N>1
  70.     % create a list of all possible combinations of N elements
  71.     [Y{N:-1:1}] = ndgrid(X) ;
  72.     % concatenate into one matrix, reshape into 2D and flip columns
  73.     Y = reshape(cat(N+1,Y{:}),[],N) ;
  74. else
  75.     % no combinations have to be made
  76.     Y = X(:) ;
  77. end

  78. % =========================================================================
  79. % Previous algorithms


  80. % Version 3.2
  81. %     % COMBN is very fast using a single matrix multiplication, without any
  82. %       explicit for-loops.
  83. %     nV = numel(V) ;
  84. %     % use a math trick
  85. %     A = [0:nV^N-1]+(1/2) ;
  86. %     B = [nV.^(1-N:0)] ;
  87. %     IND = rem(floor((A(:) * B(:)')),nV) + 1 ;
  88. %     M = V(IND) ;      

  89. % Version 2.0
  90. %     for i = N:-1:1
  91. %         X = repmat(1:nV,nV^(N-i),nV^(i-1));
  92. %         IND(:,i) = X(:);
  93. %     end
  94. %     M = V(IND) ;

  95. % Version 1.0
  96. %     nV = numel(V) ;
  97. %     % don waste space, if only one output is requested
  98. %     [IND{1:N}] = ndgrid(1:nV) ;
  99. %     IND = fliplr(reshape(cat(ndims(IND{1}),IND{:}),[],N)) ;
  100. %     M = V(IND) ;


  101. % Combinations using for-loops
  102. % can be implemented in C or VB
  103. % nv = length(V) ;
  104. % C = zeros(nv^N,N) ; % declaration
  105. % for ii=1:N,     
  106. %     cc = 1 ;
  107. %     for jj=1:(nv^(ii-1)),
  108. %         for kk=1:nv,
  109. %             for mm=1:(nv^(N-ii)),
  110. %                 C(cc,ii) = V(kk) ;
  111. %                 cc = cc + 1 ;
  112. %             end
  113. %         end
  114. %     end
  115. % end  

复制代码
下面的程序来自mathworks,可以实现这个功能

评分

1

查看全部评分

发表于 2011-5-31 15:14 | 显示全部楼层
补充俩函数:
fullfact
ff2d

评分

1

查看全部评分

发表于 2011-6-4 00:30 | 显示全部楼层
路过看下。。
发表于 2011-6-4 10:25 | 显示全部楼层
学习学习!顺便帮忙顶下!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-25 08:21 , Processed in 0.095591 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表