如何生成具有随机条目且行和列受约束的矩阵? [英] How to generate a matrix with random entries and with constraints on row and columns?

查看:146
本文介绍了如何生成具有随机条目且行和列受约束的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成一个矩阵,该矩阵的条目是零到一之间(含附加约束)的随机实数:每行的总和必须小于或等于一,并且每一列的总和必须小于或等于到一个.

How to generate a matrix that its entries are random real numbers between zero and one inclusive with the additional constraint : The sum of each row must be less than or equal to one and the sum of each column must be less than or equal to one.

示例:

matrix = [0.3, 0.4, 0.2;
          0.7, 0.0, 0.3; 
          0.0, 0.5, 0.1]

推荐答案

如果您希望矩阵均匀分布并满足这些约束,则可能需要拒绝方法.在Matlab中将是:

If you want a matrix that is uniformly distributed and fulfills those constraints, you probably need a rejection method. In Matlab it would be:

n = 3;
done = false;
while ~done
    matrix = rand(n);
    done = all(sum(matrix,1)<=1) & all(sum(matrix,2)<=1);
end

请注意,这对于大型n来说会很慢.

Note that this will be slow for large n.

这篇关于如何生成具有随机条目且行和列受约束的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆