随机选择一个二元矩阵 - Matlab
我正在处理较大的二进制矩阵,目前最大为 100x100。
假设我正在使用 30x30 二进制矩阵。那么一共有2^(30x30)个二元矩阵。我想随机选择一个二元矩阵,其中每个 2^(30x30) 矩阵都有相同的被选择概率。
我的解决方案尝试是使用函数 randi(n) 和 n = 2^(30x30) 选择 1 到 2^(30x30) 之间的数字,然后将结果转换为适当的二进制矩阵。我遇到的问题是 randi(n) 的 n 值不大于 2^54。一般来说,Matlab 似乎不喜欢很大的数字。
有什么建议吗?
I am working with largish binary matrices, at the moment up to 100x100.
Lets say I am working with 30x30 binary matrices. Then there are a total of 2^(30x30) binary matrices. I want to select a binary matrix at random, where each of the 2^(30x30) matrices has the same probability of being selected.
My solution attempt was to pick a number between 1 and 2^(30x30) using the function randi(n) with n = 2^(30x30) and then converting the result to the appropriate binary matrix. The problem I ran into was that randi(n) does not take values for n larger than 2^54. Matlab in general does not seem to like very large numbers.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果每个布尔矩阵具有相等的概率,则该矩阵的每个元素都具有相等的 0 和 1 概率。您可以用 n² 均匀随机布尔值填充适当大小的矩阵。
我手头没有 MATLAB,但在 Octave 中您可以执行类似
unidrnd(2, n, n) - 1
的操作。If each matrix of booleans has equal probability, then the elements of the matrix each have equal probability of 0 and 1. You can just fill a matrix of the appropriate size with n² uniform random booleans.
I don't have MATLAB handy, but in Octave you'd do something like
unidrnd(2, n, n) - 1
.您可以在 [0 1] 范围内使用
randint
:您还可以使用
rand
并对结果矩阵进行阈值处理:编辑:刚刚意识到它也与
randi
一起使用,语法如下:You can use
randint
in the range [0 1]:You can also use
rand
and threshold the resulting matrix:EDIT: just realized it also works with
randi
with the following syntax: