【问题标题】:Logical Question逻辑问题
【发布时间】:2011-02-04 22:46:07
【问题描述】:

考虑一个 [4x8] 矩阵“A”和 [1x8] 矩阵“B”。我需要检查是否存在这样的值“X”

[A]^T * [X] = [B]^T  exists for any x >= 0 { X is a [4X1] matrix,  T = transpose }

现在这是技巧/乏味的部分。矩阵 A 的对角线总是 1。 A11,A22,A33,A44 = 1 这个矩阵可以被认为是两半,前半部分是前 4 列,后半部分是后 4 列,如下所示:

        1 -1 -1 -1   1 0 0 1
  A =  -1  1 -1  0   0 1 0 0
       -1 -1  1  0   1 0 0 0 
       -1 -1 -1  1   1 1 0 0

前半部分的每一行可以有两个或三个 -1,如果它有两个 -1,那么下半部分的相应行应该有一个“1”,或者如果任何一行有三个 -1,则后半部分矩阵应该有两个 1。总体目标是让每一行的总和为 0。

现在 B 是一个 [1x8] 矩阵,也可以被视为如下两半:

B = -1 -1 0 0   0 0 1 1

这里前半部分可以有一个、两个、三个或四个-1,后半部分应该有相同数量的1。应该组合完成例如,如果前半部分有两个 -1,则可以将它们放置在 4 选择 2 = 6 种方式中,并且对于它们中的每一种,下半部分将有 6 种方式放置 1,这总共有 6*6 = 36 种方式。即如果前半部分有两个 -1,则 B 有 36 个不同的值。矩阵 A 中 1 的位置也应该相同。我能想到的方法是考虑valarray 或类似的东西并制作矩阵A 和B,但我不知道该怎么做。

现在对于每个 A,我必须用 B 的每个组合对其进行测试,看看是否存在

[A]^T * [X] = [B]^T 

我试图证明我得到的结果我需要知道这样的 X 是否存在。我对实现这一点感到非常困惑。欢迎任何建议。这将属于数学中的线性规划概念。我想要它在 C++ 或 Matlab 中。任何其他语言也可以接受,但我只熟悉这两种语言。提前致谢。

更新:

这是我对这个问题的回答:

clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%# Generating all possible values of vector B

%# permutations using dec2bin (start from 17 since it's the first solution)
vectorB = str2double(num2cell(dec2bin(17:255)));

%# changing the sign in the first half, then check that the total is zero
vectorB(:,1:4) = - vectorB(:,1:4);
vectorB = vectorB(sum(vectorB,2)==0,:);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%# generate all possible variation of first/second halves
z = -[0 1 1; 1 0 1; 1 1 0; 1 1 1]; n = -sum(z,2);
h1 = {
    [         ones(4,1) z(:,1:3)] ;
    [z(:,1:1) ones(4,1) z(:,2:3)] ;
    [z(:,1:2) ones(4,1) z(:,3:3)] ;
    [z(:,1:3) ones(4,1)         ] ;
};
h2 = arrayfun(@(i) unique(perms([zeros(1,4-i) ones(1,i)]),'rows'), (1:2)', ...
    'UniformOutput',false);

%'# generate all possible variations of complete rows
rows = cell(4,1);
for r=1:4
    rows{r} = cell2mat( arrayfun( ...
        @(i) [ repmat(h1{r}(i,:),size(h2{n(i)-1},1),1) h2{n(i)-1} ], ...
        (1:size(h1{r},1))', 'UniformOutput',false) );
end

%'# generate all possible matrices (pick one row from each to form the matrix)
sz = cellfun(@(M)1:size(M,1), rows, 'UniformOutput',false);
[X1 X2 X3 X4] = ndgrid(sz{:});
matrices = cat(3, ...
    rows{1}(X1(:),:), ...
    rows{2}(X2(:),:), ...
    rows{3}(X3(:),:), ...
    rows{4}(X4(:),:) );
matrices = permute(matrices, [3 2 1]);              %# 4-by-8-by-104976
A = matrices;
clear matrices X1 X2 X3 X4 rows h1 h2 sz z n r
options = optimset('LargeScale','off','Display','off');
for i = 1:size(A,3),
    for j = 1:size(vectorB,1),
        X = linprog([],[],[],A(:,:,i)',vectorB(j,:)');
        if(size(X,1)>0)  %# To check that it's not an empty matrix
            if((size(find(X < 0),1)== 0)) %# to check the condition X>=0
                if (A(:,:,i)'* X == (vectorB(j,:)'))                
                    X
                end
            end
        end
    end
end

我在 stackoverflow 的帮助下得到了它。唯一的问题是 linprog 函数在每次迭代中都会抛出很多异常以及产生的答案。例外是:

(1)Exiting due to infeasibility: an all-zero row in the constraint matrix does not have a zero in corresponding right-hand-side entry. 
(2) Exiting: One or more of the residuals, duality gap, or total relative error has stalled: the primal appears to be infeasible (and the dual unbounded).(The dual residual < TolFun=1.00e-008.

这是什么意思。我该如何克服这个问题?

【问题讨论】:

  • 是 X 一个向量 - 然后它是一个线性方程组。否则就没有意义。
  • 您可以使用最小二乘法来获得粗略的解决方案或消除线性相关性,以希望获得 4x4 矩阵或在进入“上”形式后对其进行暴力破解。 en.wikipedia.org/wiki/Overdetermined_system
  • Matlab 和编程一般不太适合证明...
  • matlab 中的 'linprog' 函数可以轻松解决这个问题,但我被困在生成组合中。 :(
  • @Sun A'[8x4] * X[8x1] = B'[4x1], A'[4x8] * X[4x1] = B'[8x1]

标签: c++ matlab logic


【解决方案1】:

从您的问题中不清楚您是否熟悉system linear equations and their solution,或者这是您试图“发明”的东西。有关 Matlab 特定说明,另请参阅 here

如果您熟悉这一点,您应该在问题中更清楚地了解是什么让您的问题与众不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 2015-07-19
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多