Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to random a matrix B to add to Matrix A with same order

Subject: How to random a matrix B to add to Matrix A with same order

From: Kittikorn Tongnimitsawat

Date: 14 Jun, 2011 09:30:21

Message: 1 of 10

I have a vector A with descending order like this

A =

0.958
0.6843
0.4391
0.329
0.012

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.

Can anyone please advise? Thanks in advance.

Subject: How to random a matrix B to add to Matrix A with same order

From: Kittikorn Tongnimitsawat

Date: 14 Jun, 2011 09:47:06

Message: 2 of 10

"Kittikorn Tongnimitsawat" wrote in message <it79nd$9i6$1@newscl01ah.mathworks.com>...
> I have a vector A with descending order like this
>
> A =
>
> 0.958
> 0.6843
> 0.4391
> 0.329
> 0.012
>
> 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.
>
> Can anyone please advise? Thanks in advance.

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 1-2 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?

Subject: How to random a matrix B to add to Matrix A with same order

From: Torsten

Date: 14 Jun, 2011 10:11:18

Message: 3 of 10

On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
wrote:
> "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...
> > I have a vector A with descending order like this
>
> > A =
>
> > 0.958
> > 0.6843
> > 0.4391
> > 0.329
> > 0.012
>
> > 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.
>
> > Can anyone please advise? Thanks in advance.
>
> 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 1-2 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?

First create a random matrix B of the same size as A with random
numbers between 0 and 1.
Then transform them according to B=-0.05+0.1*B to get random numbers
between -0.05 and 0.05.
Then use the sort-command to sort them in descending order.

Best wishes
Torsten.

Subject: How to random a matrix B to add to Matrix A with same order

From: Kittikorn Tongnimitsawat

Date: 14 Jun, 2011 14:04:08

Message: 4 of 10

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <5c260e58-4349-4989-8e00-c7ef4059795d@v31g2000vbs.googlegroups.com>...
> On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
> wrote:
> > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...
> > > I have a vector A with descending order like this
> >
> > > A =
> >
> > > 0.958
> > > 0.6843
> > > 0.4391
> > > 0.329
> > > 0.012
> >
> > > 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.
> >
> > > Can anyone please advise? Thanks in advance.
> >
> > 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 1-2 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?
>
> First create a random matrix B of the same size as A with random
> numbers between 0 and 1.
> Then transform them according to B=-0.05+0.1*B to get random numbers
> between -0.05 and 0.05.
> Then use the sort-command to sort them in descending order.
>
> Best wishes
> Torsten.

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.

:( Hope there is a solution in the space..........

Cheers!

Subject: How to random a matrix B to add to Matrix A with same order

From: Shawn Bonneau

Date: 14 Jun, 2011 14:10:05

Message: 5 of 10

Use the sort command on C, after you have already added the B matrix.

"Kittikorn Tongnimitsawat" <kittikornt@hotmail.com> wrote in message
news:it7poo$mvk$1@newscl01ah.mathworks.com...
> Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
> <5c260e58-4349-4989-8e00-c7ef4059795d@v31g2000vbs.googlegroups.com>...
>> On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
>> wrote:
>> > "Kittikorn Tongnimitsawat" wrote in message
>> > <it79nd$9i...@newscl01ah.mathworks.com>...
>> > > I have a vector A with descending order like this
>> >
>> > > A =
>> >
>> > > 0.958
>> > > 0.6843
>> > > 0.4391
>> > > 0.329
>> > > 0.012
>> >
>> > > 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.
>> >
>> > > Can anyone please advise? Thanks in advance.
>> >
>> > 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 1-2 hours to find the solution. and I have to do
>> > like this for 50000 times for my montecarlo simulation. Is there any
>> > better solution?
>>
>> First create a random matrix B of the same size as A with random
>> numbers between 0 and 1.
>> Then transform them according to B=-0.05+0.1*B to get random numbers
>> between -0.05 and 0.05.
>> Then use the sort-command to sort them in descending order.
>>
>> Best wishes
>> Torsten.
>
> 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.
>
> :( Hope there is a solution in the space..........
> Cheers!

Subject: How to random a matrix B to add to Matrix A with same order

From: Kittikorn Tongnimitsawat

Date: 14 Jun, 2011 14:22:04

Message: 6 of 10

Hi Shawn, thanks for your comment. The position on A and C has a meaning.

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.

Thanks!
Kittikorn

"Shawn Bonneau" <sbonneau@mathworks.com> wrote in message <it7q3t$o4a$1@newscl01ah.mathworks.com>...
> Use the sort command on C, after you have already added the B matrix.
>
> "Kittikorn Tongnimitsawat" <kittikornt@hotmail.com> wrote in message
> news:it7poo$mvk$1@newscl01ah.mathworks.com...
> > Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
> > <5c260e58-4349-4989-8e00-c7ef4059795d@v31g2000vbs.googlegroups.com>...
> >> On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
> >> wrote:
> >> > "Kittikorn Tongnimitsawat" wrote in message
> >> > <it79nd$9i...@newscl01ah.mathworks.com>...
> >> > > I have a vector A with descending order like this
> >> >
> >> > > A =
> >> >
> >> > > 0.958
> >> > > 0.6843
> >> > > 0.4391
> >> > > 0.329
> >> > > 0.012
> >> >
> >> > > 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.
> >> >
> >> > > Can anyone please advise? Thanks in advance.
> >> >
> >> > 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 1-2 hours to find the solution. and I have to do
> >> > like this for 50000 times for my montecarlo simulation. Is there any
> >> > better solution?
> >>
> >> First create a random matrix B of the same size as A with random
> >> numbers between 0 and 1.
> >> Then transform them according to B=-0.05+0.1*B to get random numbers
> >> between -0.05 and 0.05.
> >> Then use the sort-command to sort them in descending order.
> >>
> >> Best wishes
> >> Torsten.
> >
> > 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.
> >
> > :( Hope there is a solution in the space..........
> > Cheers!

Subject: How to random a matrix B to add to Matrix A with same order

From: Torsten

Date: 14 Jun, 2011 14:44:03

Message: 7 of 10

On 14 Jun., 16:04, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
wrote:
> Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <5c260e58-4349-4989-8e00-c7ef40597...@v31g2000vbs.googlegroups.com>...
> > On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
> > wrote:
> > > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...
> > > > I have a vector A with descending order like this
>
> > > > A =
>
> > > > 0.958
> > > > 0.6843
> > > > 0.4391
> > > > 0.329
> > > > 0.012
>
> > > > 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.
>
> > > > Can anyone please advise? Thanks in advance.
>
> > > 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 1-2 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?
>
> > First create a random matrix B of the same size as A with random
> > numbers between 0 and 1.
> > Then transform them according to B=-0.05+0.1*B to get random numbers
> > between -0.05 and 0.05.
> > Then use the sort-command to sort them in descending order.
>
> > Best wishes
> > Torsten.
>
> 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.
>
> :( Hope there is a solution in the space..........
>
> Cheers!- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

I misread your request as to 'B should have the descending order like
A'.
Sorry for the confusion.

Best wishes
Torsten.

Subject: How to random a matrix B to add to Matrix A with same order

From: Kittikorn Tongnimitsawat

Date: 14 Jun, 2011 14:47:04

Message: 8 of 10

No problem Sie! :)

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <90cf4d89-4509-4252-8a29-7646f1027740@a10g2000vbz.googlegroups.com>...
> On 14 Jun., 16:04, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
> wrote:
> > Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <5c260e58-4349-4989-8e00-c7ef40597...@v31g2000vbs.googlegroups.com>...
> > > On 14 Jun., 11:47, "Kittikorn Tongnimitsawat" <kittiko...@hotmail.com>
> > > wrote:
> > > > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i...@newscl01ah.mathworks.com>...
> > > > > I have a vector A with descending order like this
> >
> > > > > A =
> >
> > > > > 0.958
> > > > > 0.6843
> > > > > 0.4391
> > > > > 0.329
> > > > > 0.012
> >
> > > > > 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.
> >
> > > > > Can anyone please advise? Thanks in advance.
> >
> > > > 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 1-2 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?
> >
> > > First create a random matrix B of the same size as A with random
> > > numbers between 0 and 1.
> > > Then transform them according to B=-0.05+0.1*B to get random numbers
> > > between -0.05 and 0.05.
> > > Then use the sort-command to sort them in descending order.
> >
> > > Best wishes
> > > Torsten.
> >
> > 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.
> >
> > :( Hope there is a solution in the space..........
> >
> > Cheers!- Zitierten Text ausblenden -
> >
> > - Zitierten Text anzeigen -
>
> I misread your request as to 'B should have the descending order like
> A'.
> Sorry for the confusion.
>
> Best wishes
> Torsten.

Subject: How to random a matrix B to add to Matrix A with same order

From: Roger Stafford

Date: 14 Jun, 2011 19:38:03

Message: 9 of 10

"Kittikorn Tongnimitsawat" wrote in message <it7amp$bms$1@newscl01ah.mathworks.com>...
> "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i6$1@newscl01ah.mathworks.com>...
> > I have a vector A with descending order like this
> >
> > A =
> >
> > 0.958
> > 0.6843
> > 0.4391
> > 0.329
> > 0.012
> >
> > 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.
> >
> > Can anyone please advise? Thanks in advance.
>
> 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 1-2 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?
- - - - - - - - - - -
  Your statement, "it takes more than 1-2 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.

  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 x-y 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 five-sided polygon again with equal area density probability. That would be equivalent to using the unconstrained selection but doing rejection.

  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.

  However, when you have more than two entries in A the problem rapidly becomes exceedingly complex. You find yourself in a n-dimensional space with an amazingly complicated n-dimensional polytope. Such a concept is inherently intimidating to the limited human mind.

  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.

  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.

  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.)

Roger Stafford

Subject: How to random a matrix B to add to Matrix A with same order

From: Kittikorn Tongnimitsawat

Date: 16 Jun, 2011 14:32:05

Message: 10 of 10

Hi Roger, thanks a million for your understanding and suggestion. I am glad that finally there is someone understands my problem :)

Best regards,
Kittikorn

"Roger Stafford" wrote in message <it8dar$s46$1@newscl01ah.mathworks.com>...
> "Kittikorn Tongnimitsawat" wrote in message <it7amp$bms$1@newscl01ah.mathworks.com>...
> > "Kittikorn Tongnimitsawat" wrote in message <it79nd$9i6$1@newscl01ah.mathworks.com>...
> > > I have a vector A with descending order like this
> > >
> > > A =
> > >
> > > 0.958
> > > 0.6843
> > > 0.4391
> > > 0.329
> > > 0.012
> > >
> > > 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.
> > >
> > > Can anyone please advise? Thanks in advance.
> >
> > 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 1-2 hours to find the solution. and I have to do like this for 50000 times for my montecarlo simulation. Is there any better solution?
> - - - - - - - - - - -
> Your statement, "it takes more than 1-2 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.
>
> 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 x-y 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 five-sided polygon again with equal area density probability. That would be equivalent to using the unconstrained selection but doing rejection.
>
> 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.
>
> However, when you have more than two entries in A the problem rapidly becomes exceedingly complex. You find yourself in a n-dimensional space with an amazingly complicated n-dimensional polytope. Such a concept is inherently intimidating to the limited human mind.
>
> 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.
>
> 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.
>
> 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.)
>
> Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us