【问题标题】:Linear combination of a matrix/vector矩阵/向量的线性组合
【发布时间】:2011-06-20 21:35:13
【问题描述】:

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

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

这里前半部分可以有一个、两个、三个或四个-1,后半部分应该有相同数量的1。应该是线性组合。

例如,如果前半部分有两个-1,则可以将它们放在4 choose 2 = 6的方式中,并且对于它们中的每一个,将有6种方式来放置两个1下半场。所以系统总共有 6*6 = 36 种方式。即如果前半部分有两个-1,则 B 有 36 个不同的值。

我该怎么做?

【问题讨论】:

  • 您的意思是要根据您的定义生成所有可能的(即16+36+16+1=69)向量?

标签: matlab matrix


【解决方案1】:

您可以先生成所有可能的 1 和 0 排列,然后丢弃多余的排列。

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

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

allB 的每一行都是B 的可能值

【讨论】:

  • 通过淘汰的方式在这里肯定更容易+1
【解决方案2】:

这是另一个解决方案:

%# generate all possible version of first half
h1 = num2cell(-(dec2bin(1:15)-'0'),2);

%# generate all possible version of second half
h2 = arrayfun(@(i) unique(perms([zeros(1,4-i) ones(1,i)]),'rows'), (1:4)', 'UniformOutput',false);

%'# number of 1s in each row of h1
n = -cellfun(@sum, h1);

%# get final result by combining h1 and h2
B = cellfun(@(a,b) [repmat(a,size(b,1),1) b], h1, h2(n), 'UniformOutput',false);
B = cell2mat(B);

结果:

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

【讨论】:

猜你喜欢
  • 2011-06-20
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多