first, -constraints-

elements must be between 4 and 12

for example,

A=[6 7 8 9], sumA=sum(A)=30;

What is number of cases of length that can be obtained so that the sum becomes 30?

For reference,

If the length of the matrix is 5, A is [5 5 6 7 7].

Answer by KSSV
on 13 Sep 2017

x0 = 4 ; x1 = 12 ;

x = x0:x1 ;

iwant = cell([],1) ;

count = 0 ;

for i = 1:length(x)

A = nchoosek(x,i) ;

thesum = sum(A,2) ;

if any(thesum==30)

count = count+1 ;

iwant{count} = A(thesum==30,:) ;

end

end

You have the possible combinations whose sum is 30 in iwant.

Brian Kim
on 13 Sep 2017

If the number of cases is up to 20 or any value,how to modify to code?

And in above case, number of cases are originally 5 (length are 3,4,5,6 and 7).

for example, if length is 7, A is [4 4 4 4 4 4 6].

KSSV
on 13 Sep 2017

You need to only provide x.....no changes required.....

Brian Kim
on 13 Sep 2017

ah~ i am sorry. i mean, some of elements can be same.

Sign in to comment.

Answer by Walter Roberson
on 13 Sep 2017

Walter Roberson
on 13 Sep 2017

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## José-Luis (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356435-how-to-obtain-the-number-of-cases-of-length-but-those-matrices-sum-must-be-30-or-something-value-on#comment_484009

## Stephen Cobeldick (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356435-how-to-obtain-the-number-of-cases-of-length-but-those-matrices-sum-must-be-30-or-something-value-on#comment_484037

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356435-how-to-obtain-the-number-of-cases-of-length-but-those-matrices-sum-must-be-30-or-something-value-on#comment_484039

Sign in to comment.