【问题标题】:Generate Weighted Random Boolean Matrix in Matlab在 Matlab 中生成加权随机布尔矩阵
【发布时间】:2013-08-24 14:09:16
【问题描述】:

我想在 MatLab 中生成一个仅由 1 和 0 组成的 5x5 矩阵,但是我希望能够设置任何值为 1 的可能性。我希望有任何元素都有 90% 的可能性是 1 而不是 0。

但是我不希望只有 90% 的 1 和 10% 的 0。我只是希望这种情况更有可能发生。例如。我仍然希望它可能是全 0,或者 50% 1 和 50% 0,只是不太可能。

感谢您的帮助。

【问题讨论】:

    标签: matlab random matrix boolean weighted


    【解决方案1】:

    我已经写了一个非常清晰简单的代码来理解:

    mat5x5 = rand(5);
    chanse_of_1s=.90% probability for 1s, just change it.
    chanse_of_0s=.10% probability for 0s, just change it.
    mat5x5 = arrayfun(@(x)sum(x >= cumsum([0, chanse_of_0s, chanse_of_1s])), mat5x5)-1
    

    换个方式!

    【讨论】:

    • 我不知道我是否做错了什么,但如果我只使用该代码,我得到的输出就是 mat5x5=2?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2016-02-15
    • 1970-01-01
    • 2014-10-22
    • 2013-02-08
    • 1970-01-01
    相关资源
    最近更新 更多