Create matrix with {0,1,-1} with cumsum constraints, specific sum and number for [1] and [-1]
Info
This question is closed. Reopen it to edit or answer.
Show older comments
Hi,
I found an interesting problem that I want to solve, that is:fill a matrix with 1, 0 and -1 randomly, so it has a specific given sum for each row with cumsum constraints. The numbers of 1 and -1 are limited as well.
For example, for a 1*30 vector, the numbers of 1 and -1 are 18 and 8 respceticely, the sum of the row is 10 while the cumsum of the row couldn't violate the range [-5, 15]. E.g, the case [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 -1 -1 -1 -1 -1 -1 -1 -1 0 0 ] and [-1 -1 -1 -1 -1 -1 -1 -1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 ] can't meet the demand since their cumsum are [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 17 16 15 14 13 12 11 10 10 10] and [-1 -2 -3 -4 -5 -6 -7 -8 -8 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 10 10] respectively, which disobey the cumsum rule.
Randomly generating matrix's and then checking the sum is too time consuming. Two possibilities; - I want to fill a matrix with a given set of 1,-1 and 0 and then shuffle randomly. - There is a funky function I do not yet know about, that does the trick.
Thanks!
Answers (2)
Image Analyst
on 24 Feb 2014
0 votes
Would this from Roger Stafford help: http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
1 Comment
li
on 24 Feb 2014
Roger Stafford
on 24 Feb 2014
Edited: Andrei Bobrov
on 24 Feb 2014
0 votes
You asked this same question earlier on Feb 19 at:
but when I gave you an answer you rejected it on the grounds of further conditions which I didn't understand - something about "priorities" given by an additional vector. Do you have further such conditions to add to your query this time?
1 Comment
li
on 24 Feb 2014
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!