向量中的每个可能的总和组合

时间:2014-12-06 21:55:37

标签: matlab

假设我有一个数字A的向量,例如:A=[1 3 5 3 9 6](A&#39; s长度&gt; = 2)和整数X=6。需要查找满足此条件的向量中存在(A[i],A[j])的{​​{1}}对i<jA[i]+A[j]=X。打印对数。

不允许使用for / while。仅允许ceilfloormodrepmatreshapesizelengthtransposesortisemptyallanyfindsummaxmin

1 个答案:

答案 0 :(得分:4)

使用repmatlengthsum -

integer1 = 6; %// One of the paramters

A_ind = 1:length(A) %// Get the indices array

%// Expand A_ind into rows and A_ind' into columns, to form a meshgrid structure
A_ind_mat1 = repmat(A_ind,[length(A) 1])
A_ind_mat2 = repmat(A_ind',[1 length(A)])   %//'

%// Expand A into rows and A' into columns, to form a meshgrid structure
A_mat1 = repmat(A,[length(A) 1])
A_mat2 = repmat(A',[1 length(A)])   %//'

%// Form the binary matrix of -> (A[i],A[j]) where i<j
cond1 = A_ind_mat1 < A_ind_mat2

%// Use the binary matrix as a logical mask to select elements from the two
%// matrices and see which element pairs satisfy -> A[i]+A[j]=X and get a
%// count of those pairs with SUM
pairs_count = sum((A_mat1(cond1) + A_mat2(cond1))==integer1)

代码运行的输出使其更清晰 -

A =
     1     3     5     3     9     6
A_ind =
     1     2     3     4     5     6
A_ind_mat1 =
     1     2     3     4     5     6
     1     2     3     4     5     6
     1     2     3     4     5     6
     1     2     3     4     5     6
     1     2     3     4     5     6
     1     2     3     4     5     6
A_ind_mat2 =
     1     1     1     1     1     1
     2     2     2     2     2     2
     3     3     3     3     3     3
     4     4     4     4     4     4
     5     5     5     5     5     5
     6     6     6     6     6     6
A_mat1 =
     1     3     5     3     9     6
     1     3     5     3     9     6
     1     3     5     3     9     6
     1     3     5     3     9     6
     1     3     5     3     9     6
     1     3     5     3     9     6
A_mat2 =
     1     1     1     1     1     1
     3     3     3     3     3     3
     5     5     5     5     5     5
     3     3     3     3     3     3
     9     9     9     9     9     9
     6     6     6     6     6     6
cond1 =
     0     0     0     0     0     0
     1     0     0     0     0     0
     1     1     0     0     0     0
     1     1     1     0     0     0
     1     1     1     1     0     0
     1     1     1     1     1     0
pairs_count =
     2

更多解释 -

采取更多措施来澄清pairs_count必须在2处的原因 -

A_mat1A_mat2中的所有值设置为不满足less than条件的零

>> A_mat1(~cond1)=0
A_mat1 =
     0     0     0     0     0     0
     1     0     0     0     0     0
     1     3     0     0     0     0
     1     3     5     0     0     0
     1     3     5     3     0     0
     1     3     5     3     9     0
>> A_mat2(~cond1)=0
A_mat2 =
     0     0     0     0     0     0
     3     0     0     0     0     0
     5     5     0     0     0     0
     3     3     3     0     0     0
     9     9     9     9     0     0
     6     6     6     6     6     0

现在,添加A_mat1A_mat2,看看你有多少6 -

>> A_mat1 + A_mat2
ans =
     0     0     0     0     0     0
     4     0     0     0     0     0
     6     8     0     0     0     0
     4     6     8     0     0     0
    10    12    14    12     0     0
     7     9    11     9    15     0

正如您所看到的,有两个6&#39},因此我们的结果已经过验证。