逻辑问题

时间:2011-02-03 17:51:17

标签: c++ matlab logic

考虑[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列,后半部分是第二列,如下所示:

        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.

这是什么意思。我怎么能克服这个?

1 个答案:

答案 0 :(得分:0)

您的问题不清楚您是否熟悉system linear equations and their solution,或者您正在尝试“发明”。有关Matlab特定的解释,另请参阅here

如果您熟悉这一点,那么您应该更清楚地了解问题的不同之处。

相关问题