我已经编写了此脚本,但是每次单击运行时,我得到的只是输出中的空白图像。当我尝试在for循环中打印列表或变量时,什么也没打印。如果我删除最后一行,我什么也没有。 似乎我的For循环未运行。谁能看到我做错了吗?
people = 100;
c = 3; (* number of connections to add to graph per person added*)
d = 1; (* Number of people assigned randomly*)
ct =3; (* number of traits person must have in common *)
region = 11; (* Position of region percentage break down list *)
m={{0.48,0.52},{0.19,0.25,0.40,0.16},{0.36,30,34},
{0.26,0.39,0.35},{0.18,32,32,18},{0.05,0.95},
{0.13,0.87},{0.34,0.49,0.17},{0.27,0.23,0.01,0.24,0.03,0.07,0.15},
{0.7,0.12,0.04,0.03,0.11},
{0.19,0.23,0.37,0.21}}; (* List of list of % breakdowns of traits*)
randomchoice[matr_]:=Table[RandomChoice[matr[[i]]
-> Range[Length[matr[[i]]]]],{i,Length[matr]}];
PickTraits := RandomChoice[Range[Length[m]-1],ct];
listoflistoftraits = Table[randomchoice[m],people];
adjmat = {};
For[j,j<Range[Length[people]],1,
commontraits = Table[listoflistoftraits[[j]],
{i,PickTraits}];
ccount = 0;
possibleconnections = {};
possibleregionconnections = {};
emptyc = {};
For[k,k<people,1,
If[Count[listoflistoftraits[[k]],commontraits] = ct,
Append[possibleconnections,k]
,Unevaluated[Sequence[]]
];
If[listoflistoftraits[[k,Length[listoflistoftraits,region]]] =
listoflistoftraits[[j,region]],
Append[possibleregionconnections,k]
,Unevaluated[Sequence[]]
]
];
selected = RandomChoice[possibleconnections,c-d];
randomselect = RandomChoice[possibleregionconnections,d];
connectionindices = Join[selected,randomselect];
emptyc = Table[If[MemberQ[connectionindices,i],1,0],
{i,people}];
Append[adjmat,emptyc]
]
AdjacencyGraph[adjmat]
答案 0 :(得分:0)
@murksiuke是正确的。 people
是Integer
,所以Range[Length[people]] == {}
。 j
未初始化为任何值,并且未在任何地方递增,因此For
循环将永远不会终止。对于k
上的循环也是如此。您可能想要类似的东西
For[j = 1, j <= people, j++, ...]
Count
的第二个参数是模式,commontraits
是Integer
的列表,因此它永远不会与列表匹配。也许你打算
Count[{listoflistoftraits[[k]]}, commontraits]
Length
仅接受一个参数,两个参数被传递
Length[listoflistoftraits, region]
不确定您的意图。