SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 725|回复: 6
打印 上一主题 下一主题

求一个组合的问题

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2008-10-15 13:12:02 | 只看该作者

求一个组合的问题

现有数据集aa
n1
a
b
c
d
e
f
g
现在想求变量n1的各种组合,比如a b,a c,a d, a e, a f,a g,abc,abd-----等等
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2008-10-15 21:30:58 | 只看该作者

Re: 求一个组合的问题

all subsets
<!-- m --><a class="postlink" href="http://listserv.uga.edu/cgi-bin/wa?A2=ind0606c&amp;L=sas-l&amp;F=&amp;S=&amp;P=3815">http://listserv.uga.edu/cgi-bin/wa?A2=i ... &amp;S=&amp;P=3815</a><!-- m -->
[code:1xko7hvu]
data aa;
        input n1 $ 1;
        call symputx('nobs', _N_);
datalines;
a
b
c
d
e
f
g
;
run;

proc transpose data=aa out=bb;
var n1;
run;
proc means data=bb noprint;
        class col1-col&amp;nobs;
        output out=pattern(drop=_type_ _freq_);
run;
[/code:1xko7hvu]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2008-10-16 07:37:12 | 只看该作者

Re: 求一个组合的问题

[code:3k1d7ljr]%let chars=abcdefg;
%let len=%length(&amp;chars);

data ahuige(keep=chars);
  do i=0 to 2**&amp;len-1;
    chars=put(&quot;&amp;chars&quot;,&amp;len&#46;&#46;);
    do j=1 to &amp;len;
      if substr(put(i,binary&amp;len&#46;&#46;),j,1)^=1 then substr(chars,j,1)=' ';
    end;
    output;
  end;
run;[/code:3k1d7ljr]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2008-10-16 11:07:17 | 只看该作者

Re: 求一个组合的问题

太强了,感谢2位兄弟
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2008-10-17 00:48:18 | 只看该作者

Re: 求一个组合的问题

I forgot binary representation. Here's another one. Is it possible to reduce the time complexity from O(n*2^n) to O(2^n) ?
[code:3b0s05xp]
proc iml;
chars = {'a' 'b' 'c' 'd' 'e' 'f' 'g'};
x = repeat(0,1,ncol(chars));
do xi=1 to 2**ncol(chars)-1;
        if x&#91;1&#93;=0 then x&#91;1&#93;=1;
        else do;
                n0 = x&#91;&gt;&#58;&lt;&#93;; *the position of first 0;
                x&#91;1&#58;(n0-1)&#93; = 0;
                x&#91;n0&#93; = 1;
        end;
        y=rowcatc((chars&#91;loc(x=1)&#93;)`);
        print x / y;
end;

quit;
[/code:3b0s05xp]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
6#
 楼主| 发表于 2008-10-17 06:45:40 | 只看该作者

Re: 求一个组合的问题

From the appearance of your source code it seems the inner loop is gone away. I have no idea about iml. But it seems the function rowcatc does this for you(correct me if I am wrong).
however, the rowcatc itself or its counterpart should have a mechanism to scan the whole vector and concatenate them conditionally.
It might not reduce the complexity substantially.  At least on my computer it takes more time to run. Anyway, it is a nice try buddy.
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2008-10-17 12:48:12 | 只看该作者

Re: 求一个组合的问题

I was just mimicking  your code, so the inner loop is still there. I think the 'loc' function needs to visit every entry of vector 'x' to identify all the 1's, also 'x[&gt;:&lt;]' may have to search the whole vector to identify the first 0.

Anyway, my question is simply STUPID, because 2^n is already so bad that adding a factor of n doesn't make it worse.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-5 01:33 , Processed in 0.107468 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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