MATLAB: How can i extend a matrix with columns of random entries under the condition that the 3 randomn numbers in column 1-3 sum up to 1

matrix arrayrandom number

Hello, I am new to this community and would be happy if somebody could help me out. I use following code:
MC=10
rand = [zeros(MC,1),sort(rand(MC,2),2),ones(MC,1)];
weight = diff(rand,[],2);
which gives me that table:
Now I would like to extend that table to a 10×174 matrix, the problem is, the entries in column 1-3 should add up to one, and that should be the same for 4-6 and 7-9….until 172-174.
I have no idea how to do that.
Thank you very much in advance.
0,157613081677548 0,657110604715631 0,185276313606821
0,905791937075619 0,0648008446849965 0,0294072182393843
0,126986816293506 0,830180131949440 0,0428330517570544
0,485375648722841 0,428000207416178 0,0866241438609806
0,632359246225410 0,167921222663391 0,199719531111200
0,0975404049994095 0,0443459336278058 0,858113661372785
0,278498218867048 0,143263063759227 0,578238717373725
0,546881519204984 0,368854005984083 0,0842644748109329
0,792207329559554 0,165299505874743 0,0424931645657024
0,959492426392903 0,00539610880637353 0,0351114648007235

Best Answer

I wrote a function for the file exchange called ‘randfixedsum’ which will accomplish the task you have in mind. It will generate all 580 triplets, each summing to 1, in a single call. You can access it at:
http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum