http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217
MATLAB Central Newsreader  How to random a matrix B to add to Matrix A with same order
Feed for thread: How to random a matrix B to add to Matrix A with same order
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 14 Jun 2011 09:30:21 +0000
How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841009
Kittikorn Tongnimitsawat
I have a vector A with descending order like this<br>
<br>
A = <br>
<br>
0.958<br>
0.6843<br>
0.4391<br>
0.329<br>
0.012<br>
<br>
I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
<br>
Can anyone please advise? Thanks in advance.

Tue, 14 Jun 2011 09:47:06 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841010
Kittikorn Tongnimitsawat
"Kittikorn Tongnimitsawat" wrote in message <it79nd$9i6$1@newscl01ah.mathworks.com>...<br>
> I have a vector A with descending order like this<br>
> <br>
> A = <br>
> <br>
> 0.958<br>
> 0.6843<br>
> 0.4391<br>
> 0.329<br>
> 0.012<br>
> <br>
> I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
> <br>
> Can anyone please advise? Thanks in advance.<br>
<br>
Actually I have tried to use a loop to random matrix B until it fits the constraint. However, if matrix A has many rows, like 50  100 rows, it takes more than 12 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?

Tue, 14 Jun 2011 10:11:18 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841011
Torsten
On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
wrote:<br>
> "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...<br>
> > I have a vector A with descending order like this<br>
><br>
> > A =<br>
><br>
> > 0.958<br>
> > 0.6843<br>
> > 0.4391<br>
> > 0.329<br>
> > 0.012<br>
><br>
> > I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
><br>
> > Can anyone please advise? Thanks in advance.<br>
><br>
> Actually I have tried to use a loop to random matrix B until it fits the constraint. However, if matrix A has many rows, like 50  100 rows, it takes more than 12 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?<br>
<br>
First create a random matrix B of the same size as A with random<br>
numbers between 0 and 1.<br>
Then transform them according to B=0.05+0.1*B to get random numbers<br>
between 0.05 and 0.05.<br>
Then use the sortcommand to sort them in descending order.<br>
<br>
Best wishes<br>
Torsten.

Tue, 14 Jun 2011 14:04:08 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841052
Kittikorn Tongnimitsawat
Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <5c260e58434949898e00c7ef4059795d@v31g2000vbs.googlegroups.com>...<br>
> On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
> wrote:<br>
> > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...<br>
> > > I have a vector A with descending order like this<br>
> ><br>
> > > A =<br>
> ><br>
> > > 0.958<br>
> > > 0.6843<br>
> > > 0.4391<br>
> > > 0.329<br>
> > > 0.012<br>
> ><br>
> > > I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
> ><br>
> > > Can anyone please advise? Thanks in advance.<br>
> ><br>
> > Actually I have tried to use a loop to random matrix B until it fits the constraint. However, if matrix A has many rows, like 50  100 rows, it takes more than 12 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?<br>
> <br>
> First create a random matrix B of the same size as A with random<br>
> numbers between 0 and 1.<br>
> Then transform them according to B=0.05+0.1*B to get random numbers<br>
> between 0.05 and 0.05.<br>
> Then use the sortcommand to sort them in descending order.<br>
> <br>
> Best wishes<br>
> Torsten.<br>
<br>
Thanks Torsten. Unfortunately by doing this, the random will be bias. Meaning to say that the first item of matrix A will be likely increasing and the last item of matrix A will be decreasing. This is from positive value in first item in Matrix B and negative value in last item.<br>
<br>
:( Hope there is a solution in the space.......... <br>
<br>
Cheers!

Tue, 14 Jun 2011 14:10:05 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841053
Shawn Bonneau
Use the sort command on C, after you have already added the B matrix.<br>
<br>
"Kittikorn Tongnimitsawat" <kittikornt@hotmail.com> wrote in message <br>
news:it7poo$mvk$1@newscl01ah.mathworks.com...<br>
> Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <br>
> <5c260e58434949898e00c7ef4059795d@v31g2000vbs.googlegroups.com>...<br>
>> On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
>> wrote:<br>
>> > "Kittikorn Tongnimitsawat" wrote in message <br>
>> > <it79nd$9i...@newscl01ah.mathworks.com>...<br>
>> > > I have a vector A with descending order like this<br>
>> ><br>
>> > > A =<br>
>> ><br>
>> > > 0.958<br>
>> > > 0.6843<br>
>> > > 0.4391<br>
>> > > 0.329<br>
>> > > 0.012<br>
>> ><br>
>> > > I would like to create matrix B with random number between 0.05 and <br>
>> > > 0.05 to add to matrix A, let say C = A + B. There is a constraint is <br>
>> > > that C has to have the descending order like A.<br>
>> ><br>
>> > > Can anyone please advise? Thanks in advance.<br>
>> ><br>
>> > Actually I have tried to use a loop to random matrix B until it fits <br>
>> > the constraint. However, if matrix A has many rows, like 50  100 rows, <br>
>> > it takes more than 12 hours to find the solution. and I have to do <br>
>> > like this for 50000 times for my montecarlo simulation. Is there any <br>
>> > better solution?<br>
>><br>
>> First create a random matrix B of the same size as A with random<br>
>> numbers between 0 and 1.<br>
>> Then transform them according to B=0.05+0.1*B to get random numbers<br>
>> between 0.05 and 0.05.<br>
>> Then use the sortcommand to sort them in descending order.<br>
>><br>
>> Best wishes<br>
>> Torsten.<br>
><br>
> Thanks Torsten. Unfortunately by doing this, the random will be bias. <br>
> Meaning to say that the first item of matrix A will be likely increasing <br>
> and the last item of matrix A will be decreasing. This is from positive <br>
> value in first item in Matrix B and negative value in last item.<br>
><br>
> :( Hope there is a solution in the space..........<br>
> Cheers!

Tue, 14 Jun 2011 14:22:04 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841055
Kittikorn Tongnimitsawat
Hi Shawn, thanks for your comment. The position on A and C has a meaning.<br>
<br>
Actually A is a covariance of two variables and I am going to do monte carlo by changing a covariance. My constraint is that A(i,1) > A(i+1,1). Meaning to say that I cannot sort matrix A nor C. <br>
<br>
Thanks!<br>
Kittikorn<br>
<br>
"Shawn Bonneau" <sbonneau@mathworks.com> wrote in message <it7q3t$o4a$1@newscl01ah.mathworks.com>...<br>
> Use the sort command on C, after you have already added the B matrix.<br>
> <br>
> "Kittikorn Tongnimitsawat" <kittikornt@hotmail.com> wrote in message <br>
> news:it7poo$mvk$1@newscl01ah.mathworks.com...<br>
> > Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <br>
> > <5c260e58434949898e00c7ef4059795d@v31g2000vbs.googlegroups.com>...<br>
> >> On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
> >> wrote:<br>
> >> > "Kittikorn Tongnimitsawat" wrote in message <br>
> >> > <it79nd$9i...@newscl01ah.mathworks.com>...<br>
> >> > > I have a vector A with descending order like this<br>
> >> ><br>
> >> > > A =<br>
> >> ><br>
> >> > > 0.958<br>
> >> > > 0.6843<br>
> >> > > 0.4391<br>
> >> > > 0.329<br>
> >> > > 0.012<br>
> >> ><br>
> >> > > I would like to create matrix B with random number between 0.05 and <br>
> >> > > 0.05 to add to matrix A, let say C = A + B. There is a constraint is <br>
> >> > > that C has to have the descending order like A.<br>
> >> ><br>
> >> > > Can anyone please advise? Thanks in advance.<br>
> >> ><br>
> >> > Actually I have tried to use a loop to random matrix B until it fits <br>
> >> > the constraint. However, if matrix A has many rows, like 50  100 rows, <br>
> >> > it takes more than 12 hours to find the solution. and I have to do <br>
> >> > like this for 50000 times for my montecarlo simulation. Is there any <br>
> >> > better solution?<br>
> >><br>
> >> First create a random matrix B of the same size as A with random<br>
> >> numbers between 0 and 1.<br>
> >> Then transform them according to B=0.05+0.1*B to get random numbers<br>
> >> between 0.05 and 0.05.<br>
> >> Then use the sortcommand to sort them in descending order.<br>
> >><br>
> >> Best wishes<br>
> >> Torsten.<br>
> ><br>
> > Thanks Torsten. Unfortunately by doing this, the random will be bias. <br>
> > Meaning to say that the first item of matrix A will be likely increasing <br>
> > and the last item of matrix A will be decreasing. This is from positive <br>
> > value in first item in Matrix B and negative value in last item.<br>
> ><br>
> > :( Hope there is a solution in the space..........<br>
> > Cheers!

Tue, 14 Jun 2011 14:44:03 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841062
Torsten
On 14 Jun., 16:04, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
wrote:<br>
> Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <5c260e58434949898e00c7ef40597...@v31g2000vbs.googlegroups.com>...<br>
> > On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
> > wrote:<br>
> > > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...<br>
> > > > I have a vector A with descending order like this<br>
><br>
> > > > A =<br>
><br>
> > > > 0.958<br>
> > > > 0.6843<br>
> > > > 0.4391<br>
> > > > 0.329<br>
> > > > 0.012<br>
><br>
> > > > I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
><br>
> > > > Can anyone please advise? Thanks in advance.<br>
><br>
> > > Actually I have tried to use a loop to random matrix B until it fits the constraint. However, if matrix A has many rows, like 50  100 rows, it takes more than 12 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?<br>
><br>
> > First create a random matrix B of the same size as A with random<br>
> > numbers between 0 and 1.<br>
> > Then transform them according to B=0.05+0.1*B to get random numbers<br>
> > between 0.05 and 0.05.<br>
> > Then use the sortcommand to sort them in descending order.<br>
><br>
> > Best wishes<br>
> > Torsten.<br>
><br>
> Thanks Torsten. Unfortunately by doing this, the random will be bias. Meaning to say that the first item of matrix A will be likely increasing and the last item of matrix A will be decreasing. This is from positive value in first item in Matrix B and negative value in last item.<br>
><br>
> :( Hope there is a solution in the space..........<br>
><br>
> Cheers! Zitierten Text ausblenden <br>
><br>
>  Zitierten Text anzeigen <br>
<br>
I misread your request as to 'B should have the descending order like<br>
A'.<br>
Sorry for the confusion.<br>
<br>
Best wishes<br>
Torsten.

Tue, 14 Jun 2011 14:47:04 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841064
Kittikorn Tongnimitsawat
No problem Sie! :)<br>
<br>
Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <90cf4d89450942528a297646f1027740@a10g2000vbz.googlegroups.com>...<br>
> On 14 Jun., 16:04, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
> wrote:<br>
> > Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <5c260e58434949898e00c7ef40597...@v31g2000vbs.googlegroups.com>...<br>
> > > On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com><br>
> > > wrote:<br>
> > > > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...<br>
> > > > > I have a vector A with descending order like this<br>
> ><br>
> > > > > A =<br>
> ><br>
> > > > > 0.958<br>
> > > > > 0.6843<br>
> > > > > 0.4391<br>
> > > > > 0.329<br>
> > > > > 0.012<br>
> ><br>
> > > > > I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
> ><br>
> > > > > Can anyone please advise? Thanks in advance.<br>
> ><br>
> > > > Actually I have tried to use a loop to random matrix B until it fits the constraint. However, if matrix A has many rows, like 50  100 rows, it takes more than 12 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?<br>
> ><br>
> > > First create a random matrix B of the same size as A with random<br>
> > > numbers between 0 and 1.<br>
> > > Then transform them according to B=0.05+0.1*B to get random numbers<br>
> > > between 0.05 and 0.05.<br>
> > > Then use the sortcommand to sort them in descending order.<br>
> ><br>
> > > Best wishes<br>
> > > Torsten.<br>
> ><br>
> > Thanks Torsten. Unfortunately by doing this, the random will be bias. Meaning to say that the first item of matrix A will be likely increasing and the last item of matrix A will be decreasing. This is from positive value in first item in Matrix B and negative value in last item.<br>
> ><br>
> > :( Hope there is a solution in the space..........<br>
> ><br>
> > Cheers! Zitierten Text ausblenden <br>
> ><br>
> >  Zitierten Text anzeigen <br>
> <br>
> I misread your request as to 'B should have the descending order like<br>
> A'.<br>
> Sorry for the confusion.<br>
> <br>
> Best wishes<br>
> Torsten.

Tue, 14 Jun 2011 19:38:03 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841133
Roger Stafford
"Kittikorn Tongnimitsawat" wrote in message <it7amp$bms$1@newscl01ah.mathworks.com>...<br>
> "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i6$1@newscl01ah.mathworks.com>...<br>
> > I have a vector A with descending order like this<br>
> > <br>
> > A = <br>
> > <br>
> > 0.958<br>
> > 0.6843<br>
> > 0.4391<br>
> > 0.329<br>
> > 0.012<br>
> > <br>
> > I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
> > <br>
> > Can anyone please advise? Thanks in advance.<br>
> <br>
> Actually I have tried to use a loop to random matrix B until it fits the constraint. However, if matrix A has many rows, like 50  100 rows, it takes more than 12 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution? <br>
          <br>
Your statement, "it takes more than 12 hours to find the solution" indicates that you are using a "rejection" scheme to achieve what you consider to be a statistically unbiased result. However it is clear that that method is not at all practical for the A vectors you wish to use, and you are hoping there is some more efficient method that would retain the same statistics and yet not require rejection.<br>
<br>
As I see it, this seems a very difficult problem. Suppose to begin with that there were only two elements to A: A = [a1,a2] where a1 >= a2. You want to randomly select two numbers x and y, each bounded in absolute value by 0.05, but constrained by a1+x>=a2+y. If you draw a square in the xy plane with .05 and +,05 bounds, the unconstrained choice of an x,y pair would be to select a point (x,y) within the square with equal area density probability. However with the constraint, one of the square's corners may be cut off by a diagonal line. The problem then would be to randomly select a point within the resulting fivesided polygon again with equal area density probability. That would be equivalent to using the unconstrained selection but doing rejection.<br>
<br>
Of course such random selection can rather easily be done using matlab's 'rand' function appropriately. One method is to divide up the polygon into convenient triangles and/or squares, and there are good methods for filling these randomly in the correct manner.<br>
<br>
However, when you have more than two entries in A the problem rapidly becomes exceedingly complex. You find yourself in a ndimensional space with an amazingly complicated ndimensional polytope. Such a concept is inherently intimidating to the limited human mind.<br>
<br>
I faced a somewhat similar problem in devising the algorithm for 'randfixedsum' for the File Exchange which randomly selects numbers which have fixed upper and lower bounds but so as to have some predetermined sum. There is an exceedingly complicated polytope there too which has to be broken up into many simplexes in order to make the random selection unbiased and yet avoid rejection. It took me something like a month of effort to succeed in this undertaking.<br>
<br>
My guess is that there is a fair chance that an algorithm could eventually be found that would solve your problem in the way you desire but so as to avoid a rejection method. However it is quite likely that a very large amount of effort would be required.<br>
<br>
I can see right away that such a proceeding would divide up the A values into separate sections of contiguous elements within each of which the intervals were all less than 0.1 in length, since each such section can be dealt with independently of all the others. (You might see how much time you would save implementing this idea in your rejection procedure.)<br>
<br>
Roger Stafford

Thu, 16 Jun 2011 14:32:05 +0000
Re: How to random a matrix B to add to Matrix A with same order
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309217#841416
Kittikorn Tongnimitsawat
Hi Roger, thanks a million for your understanding and suggestion. I am glad that finally there is someone understands my problem :) <br>
<br>
Best regards,<br>
Kittikorn<br>
<br>
"Roger Stafford" wrote in message <it8dar$s46$1@newscl01ah.mathworks.com>...<br>
> "Kittikorn Tongnimitsawat" wrote in message <it7amp$bms$1@newscl01ah.mathworks.com>...<br>
> > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i6$1@newscl01ah.mathworks.com>...<br>
> > > I have a vector A with descending order like this<br>
> > > <br>
> > > A = <br>
> > > <br>
> > > 0.958<br>
> > > 0.6843<br>
> > > 0.4391<br>
> > > 0.329<br>
> > > 0.012<br>
> > > <br>
> > > I would like to create matrix B with random number between 0.05 and 0.05 to add to matrix A, let say C = A + B. There is a constraint is that C has to have the descending order like A.<br>
> > > <br>
> > > Can anyone please advise? Thanks in advance.<br>
> > <br>
> > Actually I have tried to use a loop to random matrix B until it fits the constraint. However, if matrix A has many rows, like 50  100 rows, it takes more than 12 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution? <br>
>           <br>
> Your statement, "it takes more than 12 hours to find the solution" indicates that you are using a "rejection" scheme to achieve what you consider to be a statistically unbiased result. However it is clear that that method is not at all practical for the A vectors you wish to use, and you are hoping there is some more efficient method that would retain the same statistics and yet not require rejection.<br>
> <br>
> As I see it, this seems a very difficult problem. Suppose to begin with that there were only two elements to A: A = [a1,a2] where a1 >= a2. You want to randomly select two numbers x and y, each bounded in absolute value by 0.05, but constrained by a1+x>=a2+y. If you draw a square in the xy plane with .05 and +,05 bounds, the unconstrained choice of an x,y pair would be to select a point (x,y) within the square with equal area density probability. However with the constraint, one of the square's corners may be cut off by a diagonal line. The problem then would be to randomly select a point within the resulting fivesided polygon again with equal area density probability. That would be equivalent to using the unconstrained selection but doing rejection.<br>
> <br>
> Of course such random selection can rather easily be done using matlab's 'rand' function appropriately. One method is to divide up the polygon into convenient triangles and/or squares, and there are good methods for filling these randomly in the correct manner.<br>
> <br>
> However, when you have more than two entries in A the problem rapidly becomes exceedingly complex. You find yourself in a ndimensional space with an amazingly complicated ndimensional polytope. Such a concept is inherently intimidating to the limited human mind.<br>
> <br>
> I faced a somewhat similar problem in devising the algorithm for 'randfixedsum' for the File Exchange which randomly selects numbers which have fixed upper and lower bounds but so as to have some predetermined sum. There is an exceedingly complicated polytope there too which has to be broken up into many simplexes in order to make the random selection unbiased and yet avoid rejection. It took me something like a month of effort to succeed in this undertaking.<br>
> <br>
> My guess is that there is a fair chance that an algorithm could eventually be found that would solve your problem in the way you desire but so as to avoid a rejection method. However it is quite likely that a very large amount of effort would be required.<br>
> <br>
> I can see right away that such a proceeding would divide up the A values into separate sections of contiguous elements within each of which the intervals were all less than 0.1 in length, since each such section can be dealt with independently of all the others. (You might see how much time you would save implementing this idea in your rejection procedure.)<br>
> <br>
> Roger Stafford