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:
Generate random matrix with constraints

Subject: Generate random matrix with constraints

From: Ryan

Date: 16 Nov, 2010 15:02:04

Message: 1 of 11

Hi,

I want to generate a random, let's say, 5 by 5 matrix, with one constraint that the sum of each row of the matrix is 1.

What's the code for that?

Thank you so much for your help!

Ryan

Subject: Generate random matrix with constraints

From: Steven_Lord

Date: 16 Nov, 2010 15:09:11

Message: 2 of 11



"Ryan " <rtang@axiomvaluation.com> wrote in message
news:ibu6dc$elk$1@fred.mathworks.com...
> Hi,
>
> I want to generate a random, let's say, 5 by 5 matrix, with one constraint
> that the sum of each row of the matrix is 1.
>
> What's the code for that?

Generate a 5-by-4 matrix using your favorite technique.
SUM along the rows of that matrix.
Subtract the sums from 1. This forms the fifth column of your matrix.

I'm guessing from the fact that you said "one" constraint that this doesn't
satisfy the other constraints you have. If so, post ALL the constraints
that you have in your next message so that people can offer suggestions on
how to satisfy them all at once (or indicate why it's impossible/extremely
difficult to do so) without having to go through several iterations.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Generate random matrix with constraints

From: Walter Roberson

Date: 16 Nov, 2010 15:10:28

Message: 3 of 11

On 16/11/10 9:02 AM, Ryan wrote:

> I want to generate a random, let's say, 5 by 5 matrix, with one
> constraint that the sum of each row of the matrix is 1.
>
> What's the code for that?

Roger Staford has posted on this topic several times, including within
the last week.


(Perhaps someone should extra Roger's answers and post them in the FAQ:
even though it is not Matlab specific, it does get asked a fair bit.)

Subject: Generate random matrix with constraints

From: Roger Stafford

Date: 16 Nov, 2010 18:20:05

Message: 4 of 11

"Ryan " <rtang@axiomvaluation.com> wrote in message <ibu6dc$elk$1@fred.mathworks.com>...
> .......
> I want to generate a random, let's say, 5 by 5 matrix, with one constraint that the sum of each row of the matrix is 1.
> ..,....
- - - - - - - - - - -
  To merely place a constraint on random variables leaves much unsaid. There needs to be specified some underlying statistical distribution to the random variables and then the constraint imposed on this distribution as a conditional probability. For example you might have said that the elements of your matrix are to be independent normal with some mean and variance but then the constraint of a fixed row sum imposed as conditional probability. Or you could say that each element is to be uniformly distributed between -10 and +10 but with the condition of row sums fixed at 0 (in which case you might make use of my 'randfixedsum'.) Or you might say they are each uniformly distributed between 0 and 1 but that you will always normalize by dividing by their sum so as to obtain a sum of 1. In this case there will be a skewing towards the center which becomes increasingly evident as the
number of elements in the row increases.

Roger Stafford

Subject: Generate random matrix with constraints

From: Ryan

Date: 16 Nov, 2010 20:18:03

Message: 5 of 11

"Steven_Lord" <slord@mathworks.com> wrote in message <ibu6qn$df9$1@fred.mathworks.com>...
>
>
> "Ryan " <rtang@axiomvaluation.com> wrote in message
> news:ibu6dc$elk$1@fred.mathworks.com...
> > Hi,
> >
> > I want to generate a random, let's say, 5 by 5 matrix, with one constraint
> > that the sum of each row of the matrix is 1.
> >
> > What's the code for that?
>
> Generate a 5-by-4 matrix using your favorite technique.
> SUM along the rows of that matrix.
> Subtract the sums from 1. This forms the fifth column of your matrix.
>
> I'm guessing from the fact that you said "one" constraint that this doesn't
> satisfy the other constraints you have. If so, post ALL the constraints
> that you have in your next message so that people can offer suggestions on
> how to satisfy them all at once (or indicate why it's impossible/extremely
> difficult to do so) without having to go through several iterations.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Steve,

Thanks for getting back!

Here is the example of the matrix that I am talking about, which is an extreme case.

[0.2,0.2,0.2,0.2,0.2
 0.2,0.2,0.2,0.2,0.2
 0.2,0.2,0.2,0.2,0.2
 0.2,0.2,0.2,0.2,0.2
 0.2,0.2,0.2,0.2,0.2]

It's a 5 by 5 matrix and the sum of each row in the matrix is 1. So it's actually one constraint for each row, 5 constraints for the matrix.

What's the best way to do this?

Thanks

Ryan

Subject: Generate random matrix with constraints

From: Ryan

Date: 16 Nov, 2010 20:22:04

Message: 6 of 11

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ibui0l$50m$1@fred.mathworks.com>...
> "Ryan " <rtang@axiomvaluation.com> wrote in message <ibu6dc$elk$1@fred.mathworks.com>...
> > .......
> > I want to generate a random, let's say, 5 by 5 matrix, with one constraint that the sum of each row of the matrix is 1.
> > ..,....
> - - - - - - - - - - -
> To merely place a constraint on random variables leaves much unsaid. There needs to be specified some underlying statistical distribution to the random variables and then the constraint imposed on this distribution as a conditional probability. For example you might have said that the elements of your matrix are to be independent normal with some mean and variance but then the constraint of a fixed row sum imposed as conditional probability. Or you could say that each element is to be uniformly distributed between -10 and +10 but with the condition of row sums fixed at 0 (in which case you might make use of my 'randfixedsum'.) Or you might say they are each uniformly distributed between 0 and 1 but that you will always normalize by dividing by their sum so as to obtain a sum of 1. In this case there will be a skewing towards the center which becomes increasingly evident as the
> number of elements in the row increases.
>
> Roger Stafford

Roger,

Thanks for getting back!

I should have asked the question better.

In this matrix that I mentioned above, I need each element varies from -1 to 1. I don't care about what kind of distribution are these elements from. The only constraint here for each row is that they need to sum up to 1.

What's the best way to structure that?

Thanks

Ryan

Subject: Generate random matrix with constraints

From: Walter Roberson

Date: 16 Nov, 2010 20:26:28

Message: 7 of 11

On 10-11-16 02:22 PM, Ryan wrote:

> I should have asked the question better.
> In this matrix that I mentioned above, I need each element varies from
> -1 to 1. I don't care about what kind of distribution are these elements
> from. The only constraint here for each row is that they need to sum up
> to 1.
>
> What's the best way to structure that?

Make each row a copy of the vector [-1 -1 1 1 1]

As you do not care about the distribution, you do not care that the
distribution is completely fixed rather than being random, and you do not care
that only the extreme values are being used.

If you *do* care about those things, then you *do* care about the distribution.

Subject: Generate random matrix with constraints

From: someone

Date: 16 Nov, 2010 20:30:06

Message: 8 of 11

"Ryan " <rtang@axiomvaluation.com> wrote in message <ibup5c$hmr$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ibui0l$50m$1@fred.mathworks.com>...
> > "Ryan " <rtang@axiomvaluation.com> wrote in message <ibu6dc$elk$1@fred.mathworks.com>...
> > > .......
> > > I want to generate a random, let's say, 5 by 5 matrix, with one constraint that the sum of each row of the matrix is 1.
> > > ..,....
> > - - - - - - - - - - -
> > To merely place a constraint on random variables leaves much unsaid. There needs to be specified some underlying statistical distribution to the random variables and then the constraint imposed on this distribution as a conditional probability. For example you might have said that the elements of your matrix are to be independent normal with some mean and variance but then the constraint of a fixed row sum imposed as conditional probability. Or you could say that each element is to be uniformly distributed between -10 and +10 but with the condition of row sums fixed at 0 (in which case you might make use of my 'randfixedsum'.) Or you might say they are each uniformly distributed between 0 and 1 but that you will always normalize by dividing by their sum so as to obtain a sum of 1. In this case there will be a skewing towards the center which becomes increasingly evident as the

> > number of elements in the row increases.
> >
> > Roger Stafford
>
> Roger,
>
> Thanks for getting back!
>
> I should have asked the question better.
>
> In this matrix that I mentioned above, I need each element varies from -1 to 1. I don't care about what kind of distribution are these elements from. The only constraint here for each row is that they need to sum up to 1.
>
> What's the best way to structure that?

OK, so what was wrong with Steven Lord's previous reply?

>
> Thanks
>
> Ryan

Subject: Generate random matrix with constraints

From: Bruno Luong

Date: 16 Nov, 2010 20:52:04

Message: 9 of 11

"Ryan " <rtang@axiomvaluation.com> wrote in message <ibup5c$hmr$1@fred.mathworks.com>...

> In this matrix that I mentioned above, I need each element varies from -1 to 1. I don't care about what kind of distribution are these elements from. The only constraint here for each row is that they need to sum up to 1.
>

Easy:

A=eye(5);
A=A(:,randperm(5))

Bruno

Subject: Generate random matrix with constraints

From: Roger Stafford

Date: 16 Nov, 2010 20:58:03

Message: 10 of 11

"Ryan " <rtang@axiomvaluation.com> wrote in message <ibup5c$hmr$1@fred.mathworks.com>...
> .......
> In this matrix that I mentioned above, I need each element varies from -1 to 1. I don't care about what kind of distribution are these elements from. The only constraint here for each row is that they need to sum up to 1.
>
> What's the best way to structure that?
> .......
- - - - - - - -
  There are many ways of distributing element values between the limits of -1 and +1. If you say you don't care about their a priori distribution as long as they satisfy these bounds, how about a uniform distribution between the limits? In that case, after placing the constraint that row elements sum to 1, you would get the kind of conditional probability results that come from the routine, 'randfixedsum', that I put in the file exchange. You can obtain it from:

 http://www.mathworks.com/matlabcentral/fileexchange/9700

  The call for your case would be:

 x = randfixedsum(m,n,s,a,b);
 x = x.';

where a = -1, b = +1, s = 1, and the desired matrix x is to be n by m in size.

Roger Stafford

Subject: Generate random matrix with constraints

From: Walter Roberson

Date: 16 Nov, 2010 21:12:10

Message: 11 of 11

On 10-11-16 02:52 PM, Bruno Luong wrote:
> "Ryan " <rtang@axiomvaluation.com> wrote in message
> <ibup5c$hmr$1@fred.mathworks.com>...
>
>> In this matrix that I mentioned above, I need each element varies from
>> -1 to 1. I don't care about what kind of distribution are these
>> elements from. The only constraint here for each row is that they need
>> to sum up to 1.
>>
>
> Easy:
>
> A=eye(5);
> A=A(:,randperm(5))

Hee, I like that.

With the information given, even just A = eye(5); would be fine, as would be

A = [ones(5,1); zeros(5,4)];

Which could be written more compactly as

A(5,5) = 0; A(:,1) = 1;

provided it was known that A did not already exist.

Tags for this Thread

No tags are associated with 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