将列设置为 0,概率为 p
我有一个尺寸为 m X n 的矩阵 A 。对于每一列 i (i > 0
和 i <= 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 > 0
and 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用函数 rand() 生成均匀分布的随机数数组,并使用逻辑索引来选择该数组小于 p 的列:
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:
您可以执行类似
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.