简体   繁体   English

满足特定条件的随机矩阵 3 x 3 生成器的算法(最好在 Python 或伪代码中)

[英]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第 1 列 column 2第 2 栏 column 3第 3 栏
a一个 b b c c
d d e e f F
g G h H i一世

should satisfies:应该满足:

  1. a + b + c = a + d + g a + b + c = a + d + g
  2. d + e + f = b + e + h d + e + f = b + e + h
  3. g + h + i = c + f + i g + h + i = c + f + i

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM