[英]Algorithm of a random matrix 3 x 3 generator that satisfies particular condition (preferably in Python, or the pseudocode)
How to generate a random 3x3 matrix that satisfies a condition that the sum of column n is equal to the sum of row n .如何生成满足n 列之和等于 n行之和的条件的随机 3x3 矩阵。
For instance, here is the illustration of matrix that satisfied the condition:例如,这里是满足条件的矩阵的说明:
column 1![]() |
column 2![]() |
column 3![]() |
---|---|---|
a![]() |
b ![]() |
c ![]() |
d ![]() |
e ![]() |
f ![]() |
g ![]() |
h ![]() |
i![]() |
should satisfies:应该满足:
I need a scratch of the pseudocode of those algorithm.我需要这些算法的伪代码。
You have 9 variables with 3 constraints, meaning you can only choose 6 of them freely, so generate 6 random numbers and calculate the rest 3.您有 9 个变量和 3 个约束,这意味着您只能自由选择其中的 6 个,因此生成 6 个随机数并计算 rest 3。
import numpy as np
mat = np.random.normal(size = (3,3))
for i in range(3):
mat[-1, i] = mat[i, :].sum() - mat[:2, i].sum()
for i in range(3):
print(mat[i, :].sum() - mat[:, i].sum())
0.0
0.0
0.0
One approach is to generate random numbers for a, b, c, d, e and f, then calculate the rest based on the equations.一种方法是为 a、b、c、d、e 和 f 生成随机数,然后根据等式计算 rest。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.