how to obtain the number of cases of length. but those matrices sum must be 30 or something value(on constraints)

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

3 Comments

Instead of asking this question again, you could have answered to the requests for clarification in this question.
When you don't, the effort of the people trying to help you is wasted.
Stephen, you should not close duplicate questions that have Answers unless the Answers also appear in the original location.

Answers (2)

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.

3 Comments

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].
You need to only provide x.....no changes required.....
ah~ i am sorry. i mean, some of elements can be same.

This question is closed.

Asked:

on 13 Sep 2017

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!