随机选择一个二元矩阵 - Matlab

发布于 2024-12-16 10:35:45 字数 283 浏览 1 评论 0原文

我正在处理较大的二进制矩阵,目前最大为 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 技术交流群。

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

发布评论

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

评论(2

客…行舟 2024-12-23 10:35:45

如果每个布尔矩阵具有相等的概率,则该矩阵的每个元素都具有相等的 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.

决绝 2024-12-23 10:35:45

您可以在 [0 1] 范围内使用 randint

matrix=randint(30,30,[0 1]);

您还可以使用 rand 并对结果矩阵进行阈值处理:

matrix=rand(30,30);
matrix=round(matrix);

编辑:刚刚意识到它也与 randi 一起使用,语法如下:

matrix=randi([0 1],30,30);

You can use randint in the range [0 1]:

matrix=randint(30,30,[0 1]);

You can also use rand and threshold the resulting matrix:

matrix=rand(30,30);
matrix=round(matrix);

EDIT: just realized it also works with randi with the following syntax:

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