使用 MATLAB 将矩阵元素概率设置为零
我有一个大小为 mXn 的矩阵 A,我想根据以下条件将其中一些元素设置为零: 我遍历矩阵的每个元素并掷一枚硬币,其成功概率为 0.3,如果成功,我将元素设置为零,否则我将继续处理下一个元素。我希望在 MATLAB 中做到这一点,并且还具有使用上述标准更改的元素的索引。我尝试使用以下内容:
B = (rand(size(A)) <= 0.3);
我不确定如何在矩阵 A 本身中启用此功能。
I have a matrix A of size mXn and I would like to set some of it elements to zero depending on the following criteria:
I go through each element of the matrix and flip a coin whose probability of success is 0.3, if there is a success I set the element to zero else I move on to the next element. I wish to do this is MATLAB and also have the indices of elements that were changed using the above criteria. I tried using the following:
B = (rand(size(A)) <= 0.3);
I am not sure how to enable this in matrix A itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者简单地说:
A( rand( size(A) ) < 0.3 ) = 0;
Or simply:
A( rand( size(A) ) < 0.3 ) = 0;
我认为你想要的是
但我可能误解了这个问题。
I think what you want is
But I may have misunderstood the question.
首先搜索符合您条件的项目
将这些项目替换为零
First search for items that match your condition
Replace those items by zero