Create matrix with {0,1,-1} with cumsum constraints, specific sum and number for [1] and [-1]

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)

1 Comment

Yes, I have read it. The problem is how to control the numbers of 1 and -1, and how to fill them randomly or according to an order (preferred). Also see my last question: http://www.mathworks.com/matlabcentral/newsreader/view_thread/334228.
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

Hi, Stafford, thank you a lot. There are further conditions added this time so I create this new query. I am sorry I make you confused. The two added conditions are: I. The total numbers of 1 and -1 are restricted, as given in my new question. II. The orders of putting 1 and -1 is not random, they obey a series (the new additional vector) this time. E.g., a vector with 30 elements obey the order [2 24 27 22 28 23 14 8 15 21 4 12 19 30 7 1 9 18 3 16 26 17 13 10 11 25 29 6 5 20], 1 is put with the priority from the beginning to the end, and -1 is put with the priority from the end to the beginning. While there is a contradiction, then the current position to fill 1 or -1 is abandoned and move to the next.
Hope you can understand, thank you again.

This question is closed.

Asked:

li
on 24 Feb 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!