【问题标题】:Matlab: Finding the permutation matrices that produce another matrixMatlab:找到产生另一个矩阵的置换矩阵
【发布时间】:2016-04-13 21:54:21
【问题描述】:

我正在尝试编写 MATLAB 代码,它可以让我找到矩阵的置换矩阵。

让我们考虑下面的例子。我得到了矩阵AB

A = [1 2 3;4 5 6; 7 8 9]   % is a given matrix
B = [9 7 8;3 1 2; 6 4 5]   % is a permuted version of A.

我的目标是找到矩阵L(预乘A)和R(后乘A)使得L*A*R = B

% L is an n by n (3 by 3) that re-order the rows a matrix when it pre-multiply that matrix
L = [0 0 1;1 0 0;0 1 0] 

% R is an n by n that re-order the columns of a matrix
R = [0 1 0;0 0 1;1 0 0] 

B = L*A*R 

当我知道AB时,如何找到LR

【问题讨论】:

标签: algorithm matlab matrix permutation combinatorics


【解决方案1】:

为了给出一个基线解决方案,这里是蛮力方法:

function [L,R] = find_perms(A,B)
    [n,n] = size(A);
    p = perms(1:n);
    I = eye(n);
    for i=1:size(p,1)
        for j=1:size(p,1)
            L = I(p(i,:),:);
            R = I(:,p(j,:));
            if isequal(L*A*R, B)
                return;
            end
        end
    end
    % none found
    L = [];
    R = [];
end

让我们测试一下:

A = [1 2 3; 4 5 6; 7 8 9];
B = [9 7 8; 3 1 2; 6 4 5];
[L,R] = find_perms(A,B);
assert(isequal(L*A*R, B));

左/右排列矩阵如预期:

>> L
L =
     0     0     1
     1     0     0
     0     1     0
>> R
R =
     0     1     0
     0     0     1
     1     0     0

【讨论】:

  • 感谢您的回复。我实际上在 Matlab 中复制并粘贴了您的代码,这就是我得到的答案,我不知道为什么会这样说。再次感谢您的回复。 “错误:文件:experience.m 行:3 列:1 函数定义在此上下文中是不允许的。”“FUNCTION 关键字在此处使用无效”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 2012-12-11
相关资源
最近更新 更多