Daniel Watson
Daniel Watson

Reputation: 63

Every possible sum combination in a vector

Assuming I'm having a vectors of numbers A, for example: A=[1 3 5 3 9 6](A's length >= 2) and an Integer X=6. Need to find how many pairs (A[i],A[j]) where i<j exist in the vector which answer this condition: A[i]+A[j]=X. The number of pairs is printed.

Not allowed to use for/while. Allowed only ceil,floor,mod,repmat,reshape,size,length,transpose,sort,isempty,all,any,find ,sum,max,min.

Upvotes: 0

Views: 261

Answers (1)

Divakar
Divakar

Reputation: 221524

With repmat, length and sum -

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)

Outputs from code run to make it clearer -

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

A bit more explanation -

Taking few more steps to clarify why pairs_count must be 2 here -

Set all values in A_mat1 and A_mat2 to be zeros that do not satisfy the less than criteria

>> 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

Now, add A_mat1 and A_mat2 and see how many 6's you got -

>> 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

As you can see there are two 6's and thus our result is verified.

Upvotes: 4

Related Questions