将列设置为 0,概率为 p

发布于 2025-01-03 13:49:19 字数 247 浏览 4 评论 0原文

我有一个尺寸为 m X n 的矩阵 A 。对于每一列 ii > 0i <= n),我想抛一枚硬币并将整个列填充为 0概率为 p 的值。在 MATLAB 中如何实现这一点?

示例:

A = [1 2 3 4; 5 6 7 8] 且 p = 0.5 可能会导致 A' = [1 0 3 0; 5 0 7 0]

I've got a matrix A with the dimensions m X n. For every column i (i > 0and i <= n) I want to flip a coin and fill the whole column with 0 values with probability p. How can this be accomplished in MATLAB?

Example:

A = [1 2 3 4; 5 6 7 8] and p = 0.5 could result in
A' = [1 0 3 0; 5 0 7 0]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2025-01-10 13:49:19

您可以使用函数 rand() 生成均匀分布的随机数数组,并使用逻辑索引来选择该数组小于 p 的列:

A = [1 2 3 4; 5 6 7 8];
p = 0.5;

A(:, rand(size(A,2), 1)<p) = 0

A =

     0     2     0     0
     0     6     0     0

You can use the function rand() to generate an array of uniformly distributed random numbers, and use logical indexing to select colums where that array is less than p:

A = [1 2 3 4; 5 6 7 8];
p = 0.5;

A(:, rand(size(A,2), 1)<p) = 0

A =

     0     2     0     0
     0     6     0     0
友谊不毕业 2025-01-10 13:49:19

您可以执行类似 bsxfun(@times, A, rand(1, size(A, 2)) > p) 的操作。不过,亚历克斯的回答无疑更好。

You can do something like bsxfun(@times, A, rand(1, size(A, 2)) > p). Alex's answer is admittedly better, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文