MATLAB:生成具有特定约束的所有可能的N x N矩阵 [英] MATLAB: Generate all possible N x N matrices with specific constraints

查看:411
本文介绍了MATLAB:生成具有特定约束的所有可能的N x N矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个初始的N×N矩阵,所有对角元素均等于零.您想要生成所有可能的N×N矩阵,例如:

Suppose you have an initial N-by-N matrix, with all diagonal elements equal to zero. You want to generate all possible N-by-N matrices such that:

  • 所有对角元素继续为零
  • 列和行将总和保留在初始矩阵中
  • 所有元素都是正整数(包括零)

例如,对于此3×3初始矩阵:

For example, for this 3-by-3 initial matrix:

0 1 3
2 0 1
3 2 0

一种可能的变化是:

0 0 4
3 0 0
2 3 0

推荐答案

答案的初步思路,当然可以进一步改进.

An initial idea for an answer, which can certainly be further improved.

您可以开始考虑一种制作对角线为零,行和列之和为零的矩阵的方法.如果可以轻松构建这些矩阵,则可以通过将所有矩阵与初始矩阵相加来获得结果.

You can start thinking of a way to make matrices that have zeros on the diagonal and where rows and columns sum to zeros. If these can be constructed easily, then you can obtain your result by adding your initial matrix with all of them.

例如:

[ 0  1 -2  1;
  1  0 -1  0;
 -1  2  0 -1;
  0 -3  3  0];

您甚至可以限制这些辅助"矩阵在每行/每一列上最多具有单个1和单个-1.其他所有的东西都可以从它们中构造出来.

You can even restrict these 'helper' matrices to have maximally a single 1 and a single -1 on each row/column. All others can be constructed from them.

例如

A = [ 0 -1  2 -1;
      2  0 -2  0;
     -2  1  0  1;
      0  0  0  0];
B = [ 0 -1  1  0;
      1  0 -1  0;
     -1  1  0  0;
      0  0  0  0];
C = [ 0  0  1 -1;
      1  0 -1  0;
     -1  0  0  1;
      0  0  0  0];
% A equals B+C

我认为这至少可以减轻您的问题.祝你好运!

I think this at least reduces your problem a bit. Good luck!

这篇关于MATLAB:生成具有特定约束的所有可能的N x N矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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