How i can create array with fixed sum for each row and column?

1 view (last 30 days)
Hello Everyone,
Please help me to write matlab code, I am trying to create 5*5 matrix containing random values having constraints....... (1) sum of elements of row 1(X1) = sum of elements of column 1 (X1) (Sum = X1<= 7)............. (2) sum of elements of row 2 =7 and sum of elements of column 2 = 0 .......... (3) sum of elements of row 3(X3) = sum elements of column 3(X3) (Sum X3 <= 7)............ (4) sum of elements of row 4 =0 and sum of elements of column 4 = 7 ................ (5) sum of elements of row 5(X5) = sum elements of column 5(X5) (X5<= 7)........
Rows act as sources nodes. columns act as destination nodes.
Actually, this matrix represents a transshipment problem which is sending 7 units of data from (starting node 2) to (termination node 4).above constraints show that data going into node 2 from all nodes is zero and going out to all nodes is 7 units. Similarly, data going into destination node 4 is 7 units and nothing comes out from this. Whereas for all the other nodes, total amount of data coming in = total amount of data coming out.
all the diagonal elements should be zero. like (1,1)...(2,2).....(5,5) and range for all elements of matrix = 0 to 7
This is what i am able to do so far....
no_sourcenode = input ('ENTER THE NUMBER OF source NODE :');
no_destiantion = input ('ENTER THE NUMBER OF destination node :');
chromosome = round(rand(no_sourcenode,no_destiantion));
chromosomes(1:(no_sourcenode+1):no_sourcenode*no_destiantion)= 0; (to make diagonal elements zero)
Thank you very much

Accepted Answer

Image Analyst
Image Analyst on 19 Aug 2014
Looks like something Roger can answer better than me. In the meantime, see his randfixedsum utility: http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum

More Answers (0)

Community Treasure Hunt

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

Start Hunting!