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

1 vue (au cours des 30 derniers jours)
reshdev
reshdev le 19 Août 2014
Commenté : Image Analyst le 21 Août 2014
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

Réponse acceptée

Image Analyst
Image Analyst le 19 Août 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
  2 commentaires
reshdev
reshdev le 19 Août 2014
ok thank you.
Image Analyst
Image Analyst le 21 Août 2014
You're welcome. Can you officiall "Accept" the answer if it answered your question?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by