# I want to obtain number of cases(matrix's length) . By applying same sum in any matrix. (on constraints)

1 visualizzazione (ultimi 30 giorni)
Brian Kim il 8 Set 2017
Commentato: Stephen23 il 13 Set 2017
for example,
A is here.
A=[6 7 8 9];
and,
sumA=sum(A)=30;
The element of the matrix that causes the sum to be 30 must be expressed as a value between 4 and 12.
When I calculate directly, the number of cases that can be obtained is 3,4,5,6,7.
In this way, how to create code?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Stephen23 il 13 Set 2017

Accedi per commentare.

### Risposte (2)

José-Luis il 8 Set 2017
If what you mean is that you want the sum of a random array of four elements to be thirty with some condition placed on the last of them:
lims = [4,12];
totVal = 30;
lastElement = randi(diff(lims) + 1,1 ) + lims(1) - 1;
valueToSplit = totVal - lastElement;
frac = rand(1,3);
frac = frac ./ sum(frac);
result = [round(frac.*valueToSplit), lastElement]
sum(result)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Andrei Bobrov il 8 Set 2017
Modificato: Andrei Bobrov il 13 Set 2017
z = 4:12;
out = [];
for jj = 1:numel(z)
k = nchoosek(z,jj);
out = [out;num2cell(k(sum(k,2) == 30,:),2)];
end
"number of rows":
numel(out)
##### 2 CommentiMostra NessunoNascondi Nessuno
Brian Kim il 12 Set 2017
Modificato: Andrei Bobrov il 13 Set 2017
i don't wanna all cases matrices. I want the number of rows I can get
José-Luis il 12 Set 2017
Modificato: José-Luis il 13 Set 2017
I don't understand what you mean. Could you explain?

Accedi per commentare.

### Categorie

Scopri di più su 선 플롯 in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!