Assigning elements for a matrix.

3 views (last 30 days)
Let us consider three matrix
A = [a11, a12, a13, a14; a21, a22, a23, a24; a31, a32, a33, a34]
B = [b11; b21; b31]
C = [c11, c12, c13, c14]
such that:
a11+a12+a13+a14<=b11
a21+a22+a23+a24<=b21
a31+a32+a33+a34<=b31
a11+a21+a31<=c11
a12+a22+a32<=c12
a13+a23+a33<=c13
a14+a24+a34<=c14
all a, b and c is a set of positive integers Value of elements of A is not known. What will be the code to create matrix A?

Accepted Answer

Walter Roberson
Walter Roberson on 7 Sep 2018
Find the minimum of the b and c values. Generate random negative integers that are all less than -abs(the minimum). The sum of the negative integers is more negative than any of the individual negative integers and so the sum cannot exceed the value indicated.
For example if all of the b and c are positive then just choose random negative integers. There are an infinite number of them so you are not generating any fewer solutions by not generating any possible solutions involving positive values.
  2 Comments
SYED ABOU ILTAF HUSSAIN
SYED ABOU ILTAF HUSSAIN on 7 Sep 2018
If all a, b and c are set of positive integers, then what will be the code?
Walter Roberson
Walter Roberson on 7 Sep 2018
Do you need all of the solutions, or just one of the solutions?
Pick the rows. For any one row, the question becomes a constrained integer partition with a fixed number of buckets, with the constraint being non-negative (which also puts an upper bound constraint): finding nonnegative integers P+Q+R+S = B . You can also use the column information to place upper bounds
The discussion at https://www.mathworks.com/matlabcentral/answers/327656-conditional-random-number-generation#answer_257296 deals with finding all or a random such integer partition.
Repeat for each row to fill out the matrix. Then you can cross-check against the column sums. If you fail, generate again.
It is possible to work iteratively on choosing the values: once you have chosen one row then the available space reduces due to column-sum constraints. However, if you do this then you will find that the distribution of values is not fair: the first row will tend to have the widest range and the last row will tend to have the narrowest range.
Perhaps Roger has some ideas on making the dual constraint sum more efficient.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!