function H=peg(length_all,length_info,d)
length_check=length_all-length_info;
H_info=zeros(length_check,length_info);
%%
%信息位的编码
record=zeros(1,length_check);
i=1;
select=randperm(length_check,d(i));%随机选取3个
H_info(select,i)=1;
record(select)=record(select)+1;
while min(record)==0%第二个输出符号开始,未全覆盖则努力
i=i+1;
wheremin=find(record==min(record));%即此时min(record)为0;
temporary=ones(1,length_check);
f=0;
while min(record)==0&&f<d(i)%未全覆盖且第i个信息位的边没加完
f=f+1;
fff=randperm(length(wheremin),1);
H_info(wheremin(fff),i)=1; %将这个对应G中的位置 置一
record(wheremin(fff))=record(wheremin(fff))+1;%度数+1
temporary(wheremin(fff))=0;%表示被选过了
wheremin=find(record==min(record));
end%全覆盖了,可以开始考虑树了
remain=d(i)-f;%第i个信息位是否添加完
if remain~=0%没玩
aaa=find(temporary==1);%还没用过的校验节点
select_remain=randperm(length(aaa),remain);%此时检验节点度都是1,所以随机
H_info(aaa(select_remain),i)=1; %将这个对应G中的位置 置一
record(aaa(select_remain))=record(aaa(select_remain))+1;%度数+1
end
end%现在所有的输入符号的度数都至少是1了,全覆盖,正确无误
then=i+1;%从下一个信息节点开始
%%
for i=then:length_info%以后的信息节点
disp(i)
select=zeros(1,d(i));%第i个信息节点选了哪些校验节点
wheremin=find(record==min(record));
ff=randperm(length(wheremin),1);%wheremin中第ff个
select(1)=wheremin(ff);%该输出符号的第一条边与度最小的输入符号相连
H_info(select(1),i)=1;
record(wheremin(ff))=record(wheremin(ff))+1;%又选了一次,度数+1
temporary=ones(1,length_check);
temporary(select(1))=0;%选过了的不能再选
for s=2:d(i)%还要选这么多个校验节点
rember=zeros(5,length_check);%记录树形
H_inof_tree=H_info;
heigh=1;%树的高度
out2in=find(H_info(:,i)==1);%与第i个信息节点(根)相连的第一层 N->K
H_inof_tree(out2in,i)=0;%拆掉,避免重复
rember(heigh,out2in)=1;%第一层
%必须用while,因为不知道树有多高
choose_record=ones(1,length_check);
choose_record(out2in)=0;
while 1
G_row=H_inof_tree(out2in,:);%这些行(校验节点)与欲求的信息节点相连,要对每一行查1,即下一层的信息节点
H_inof_tree(out2in,:)=0;%拆掉该输入节点
if length(out2in)==1
in2out=find(G_row==1);
else
in2out=find(max(G_row)==1);
end
if isempty(in2out)
break;
end
G__clo=H_inof_tree(:,in2out);%1 2 3
H_inof_tree(:,in2out)=0;
G_col=G__clo';
if length(in2out)==1
inter_out2in=find(G_col==1);
else
inter_out2in=find(max(G_col)==1);
end
if isempty(inter_out2in)
break;
end
heigh=heigh+1;%都不空,树高度+1
out2in_old=inter_out2in;
out2in=inter_out2in;%本来在这里+1
rember(heigh,out2in_old)=1;
choose_record(out2in_old)=0;
end
if max(choose_record)==1%有校验节点不在树里
not_in_tree=find(choose_record==1);%这些节点不在树中
not_in_tree_min=find(record(not_in_tree)==min(record(not_in_tree)));
fff=randperm(length(not_in_tree_min),1);
select(s)=not_in_tree(not_in_tree_min(fff));
else%校验节点都在树里,在树里找
longest=find(rember(heigh,:)==1);
in_tree_min=find(record(longest)==min(record(longest)));
fff=randperm(length(in_tree_min),1);
select(s)=longest(in_tree_min(fff));
end
H_info(select(s),i)=1;
record(select(s))=record(select(s))+1;
temporary(select(s))=0;
end
end
ss=ones(1,length_check);
H_check=diag(ss);%需要是一个三角矩阵,上或者下
for i=1:length_check-1
H_check(i+1,i)=1;
end
H=[H_info H_check];