声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1071|回复: 4

[编程技巧] 请教一个排列问题

[复制链接]
发表于 2010-1-5 21:37 | 显示全部楼层 |阅读模式

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

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

x
从n个不同的球中拿球,拿完后放回,拿m次,列出所有的可能情况

拿法应该一共有n^m种,如何用Matlab实现?

[ 本帖最后由 ChaChing 于 2010-1-6 08:26 编辑 ]
回复
分享到:

使用道具 举报

发表于 2010-1-5 21:53 | 显示全部楼层

回复 楼主 yanice 的帖子

水平专业有限, 不太确定LZ要什么?
建议楼主举例说清楚些
 楼主| 发表于 2010-1-5 21:58 | 显示全部楼层
比如有【1,2,3】三个数,可能的排列有,每次从上面三个数中拿一个出来,一共拿4次,组成一个数列例如【1 2 3 1】
【1 1 2 1】
【1 2 3 2】


[ 本帖最后由 friendchj 于 2010-1-7 02:13 编辑 ]
发表于 2010-1-6 08:30 | 显示全部楼层
好像没现成的函数可用!
一时想不出好法子, 但m不大时, 使用for loop应该也能执行!
待高人路过
发表于 2010-1-6 08:56 | 显示全部楼层
刚搜了下官网, 这个应该适用!
http://www.mathworks.com/matlabc ... ge/26242-vchoosekro
or
http://www.mathworks.com/matlabcentral/fileexchange/12724-picking-elements-from-a-set

  1. % pick    Picking elements from a set (combinations, permutations)
  2. %
  3. %   s = pick(V,k,Type)
  4. %
  5. %   Gives all possibilities of picking k elements from the
  6. %   set V with or without order and repetition. V can be an
  7. %   array of any size and any type.
  8. %
  9. %   Type can have the following values: '', 'o', 'r', 'or'.
  10. %     'o' means pick ordered set of k elements
  11. %     'r' means replace elements after picking
  12. %
  13. %   s is an array with all picks, one subset per row.
  14. %
  15. %   Examples
  16. %     pick(1:2,5,'or'), pick('abcd',2,''), pick(-1:1,4,'r'), pick('X':'Z',3,'o')
  17. % Stefan Stoll, ETH Zurich, 20 October 2006
  18. function s = pick(V,k,Type)
  19. errThirdMissing = 'Third argument Type ('''', ''o'', ''r'', or ''or'') is missing!';
  20. errThreeExpected = 'Three arguments (V, k, Type) must be provided.';
  21. switch nargin
  22.   case 3,
  23.   case 2, error(errThirdMissing);
  24.   case 1,
  25.     if strcmp(V,'test'), pick_test; return;
  26.     else error(errThreeExpected);
  27.     end
  28.   case 0, help(mfilename); return;
  29.   otherwise, error(errThreeExpected);
  30. end
  31. N = numel(V);
  32. if (N==0), error('First argument V must be an array with at least one element.'); end
  33. if (numel(k)~=1) || rem(k,1) || (k<1)
  34.   k, error('Second argument k must be a positive integer. You gave the above.');
  35. end
  36. if ~ischar(Type), Type, error('Third argument must be a string.'); end
  37. if isempty(strfind(Type,'r')) && (k>N)
  38.   str = sprintf('Picking elements without repetition:\n  k must not be larger than the number of elements in V.\n');
  39.   error([str 'You gave k=%d for %d elements in V.'],k,N);
  40. end
  41. switch sort(Type)
  42.   case '',   idx = combinations_without_repetition(N,k);
  43.   case 'o',  idx = permutations_without_repetition(N,k);
  44.   case 'r',  idx = combinations_with_repetition(N,k);
  45.   case 'or', idx = permutations_with_repetition(N,k);
  46.   otherwise
  47.     Type
  48.     error('Third argument Type must be one of '''', ''o'', ''r'', ''or''.');
  49. end
  50. s = V(idx); if (k==1), s = s(:); end
  51. return
  52. %=====================================================================
  53. function m = combinations_with_repetition(N,k)
  54. if (k==1), m = (1:N).'; return; end
  55. if (N==1), m = ones(1,k); return; end
  56. m = [];
  57. for q = 1:N
  58.   mnext = combinations_with_repetition(N+1-q,k-1);
  59.   m = [m; q*ones(size(mnext,1),1), mnext+q-1];
  60. end
  61. %===================================================================
  62. function p = permutations_without_repetition(N,k)
  63. p = permutations_with_repetition(N,k); ps = sort(p.').';
  64. idx = any(ps(:,2:end)==ps(:,1:end-1),2); p(idx,:) = [];
  65. %===================================================================
  66. function s = permutations_with_repetition(N,k)
  67. if (k==1), s = (1:N).'; return; end
  68. if (N==1), s = ones(1,k); return; end
  69. [idx{1:k}] = ndgrid(1:N);
  70. s = fliplr(reshape(cat(ndims(idx{1}),idx{:}),[],k));
  71. %===================================================================
  72. function c = combinations_without_repetition(N,k)
  73. if (N>1), c = nchoosek(1:N,k);
  74. else c = 1; end
  75. %===================================================================
  76. function pick_test
  77. disp('=========== pick() tests ======================'); Nmax = 6;
  78. Type = {'','o','r','or'}; Repetition = [0 0 1 1];
  79. Name{1} = 'Combinations without repetition'; Name{2} = 'Permutations without repetition';
  80. Name{3} = 'Combinations with repetition'; Name{4} = 'Permutations with repetition';
  81. for t = 1:4, disp(' '); disp(Name{t});
  82.   for N = 1:Nmax, if Repetition(t), kmax = Nmax; else kmax = N; end
  83.     for k = 1:kmax
  84.       s = pick(uint8(1:N),k,Type{t}); m1 = size(s,1); k1 = size(s,2);
  85.       switch t
  86.         case 1, m = nchoosek(N,k); case 2, m = prod(N-k+1:N);
  87.         case 3, m = nchoosek(N+k-1,k); case 4, m = N^k;
  88.       end
  89.       fprintf('  N=%d, k=%d, expected %dx%d, found %dx%d\n',N,k,m,k,m1,k1);
  90.       if (m1~=m) | (k1~=k), error('Unexpected size of output array!'); end
  91.     end
  92.   end
  93. end
  94. disp('All tests passed!');
复制代码

[ 本帖最后由 ChaChing 于 2010-1-6 10:22 编辑 ]
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-6-7 04:30 , Processed in 0.059658 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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