如何从列表中选择值。数学
How to choose values from a list. Mathematica
我有这个代码:
x = {1, 2, 3, 4, 5}; p = {0.5, 0.3, 0.1, 0.06, 0.04};
Do[
n = RandomVariate[PoissonDistribution[30]]; M = Table[0, {n}];
Do[
U = RandomReal[]; i = 1;
While[U > Sum[p[[j]], {j, 1, i}], i = i + 1];
X = x[[i]]; M[[j]] = X;
, {j, 1, n}];
Daytotal[k] = M
, {k, 1, 365}]
W = Table[Total[Daytotal[m]], {m, 1, 365}];
N[Mean[W]]
Histogram[W, Automatic, "PDF"]
我想从 W 中选择大于 80 的值。我已经用命令 While 尝试了所有我知道的方法,但我找不到结果。
两者都
greaterthan80={};
i=1;
While[i<=365,
If[W[[i]]>80,
AppendTo[greaterthan80,W[[i]]]
];
i++
];
greaterthan80
或
isitgreater[x_]:=x>80;
greaterthan80=Select[W,isitgreater]
或者只是
greaterthan80=Select[W,#>80&]
我有这个代码:
x = {1, 2, 3, 4, 5}; p = {0.5, 0.3, 0.1, 0.06, 0.04};
Do[
n = RandomVariate[PoissonDistribution[30]]; M = Table[0, {n}];
Do[
U = RandomReal[]; i = 1;
While[U > Sum[p[[j]], {j, 1, i}], i = i + 1];
X = x[[i]]; M[[j]] = X;
, {j, 1, n}];
Daytotal[k] = M
, {k, 1, 365}]
W = Table[Total[Daytotal[m]], {m, 1, 365}];
N[Mean[W]]
Histogram[W, Automatic, "PDF"]
我想从 W 中选择大于 80 的值。我已经用命令 While 尝试了所有我知道的方法,但我找不到结果。
两者都
greaterthan80={};
i=1;
While[i<=365,
If[W[[i]]>80,
AppendTo[greaterthan80,W[[i]]]
];
i++
];
greaterthan80
或
isitgreater[x_]:=x>80;
greaterthan80=Select[W,isitgreater]
或者只是
greaterthan80=Select[W,#>80&]