I want to obtain number of cases(matrix's length) . By applying same sum in any matrix. (on constraints)
4 views (last 30 days)
A is here.
A=[6 7 8 9];
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?
José-Luis on 8 Sep 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]