COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Generate random matrix with constraints

• Email
• Follow

```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
```
 0

See related articles to this posting

```
"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
http://www.mathworks.com

```
 0

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

```"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
```
 0

```"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
> 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
```
 0

```"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
```
 0

```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.
```
 0

```"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
```
 0

```"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
```
 0

```"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
```
 0

```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.
```
 0

10 Replies
650 Views

Similar Articles

12/20/2013 5:45:36 AM
[PageSpeed]

Similar Artilces:

Generate Random Matrix (mxn)
Hello, I'd like to generate a random matrix (e.g. 1000x10) with numbers in the interval [1 49].However,I'd like to incorporate at least one of the two following restrictions: a. Each row must have unique elements (strong restriction-must be satisfied!!!) b. Matrix must have unique rows (non-repeatable rows) (less significant restriction). I'd manage to implement this using a for-loop.However,due to the fact that I'm currently vectorizing my entire code,I'd appreciate it if anybody could point out a solution without using a for-loop... Thanx in advance... Vassilios.....

Regexp as constraint on random string generator
hi, I guess this is a little OT but I suspect the right expertise is as readily available here as anywhere... I was asked to generate a large number of random strings conforming to a certain pattern. The pattern was rather easily described using a regexp, and it crossed my mind to ask whether there is any way to use a regexp to "drive" the random construction of a string. For my particular problem the string's length was known, so one possible approach was to create random strings of the right length and use the regexp to filter them. But obviously that's inefficient an...

Random matrix of 0s and 1s with two not trivial constraints
Hi Guys, I need to generate a random matrix of K columns and N rows containing 0 and 1 such that a) each row contains exactly k ones b) each row is different from the other (combinatorics imposes that if N > nchoosek(K,k) there will be nchoosek(K,k) rows) Assume I want N=10000 (out of all the possible nchoosek(K,k)=27405 combinations) different 1 x K (with K=30), vectors containing k (with k=4) ones and K-k zeros . This code clear all; close N=10000; K=30; k=4; M=randi([0 1],N,K); plot(sum(M,2)) % condition a) not satisfied does not satisfy neither a) nor b). This...

how to generate a random matrix with mean 'x'
How to generate a random matrix with integer values ranging from 'm' to 'n' and corresponding mean 'x'??? "res " <res.mac2000@gmail.com> wrote in message <h33val\$5tn\$1@fred.mathworks.com>... > How to generate a random matrix with integer values ranging from 'm' to 'n' and corresponding mean 'x'??? How many numbers? And is m < x < n ? Jos "Jos " <#10584@fileexchange.com> wrote in message <h345l5\$nc6\$1@fred.mathworks.com>... > "res " <res.mac2000@gmail.com> wrote in messa...

How to sample a random signal generated by random generator?
I am new to LabVIEW. Please provide me better solution. Hello Project Support, Thank you for contacting National Instruments. A basic data acquisition system consists of a signal (usually from a transducer), signal conditioning, a DAQ device, and driver level / application level software. What data acquisition system are you currently using? If you can provide a little more information, I will be happy to help you put together data acquisition code in LabVIEW. The following information would be helpful: 1) Type of DAQ card (if any) 2) Version of NI-DAQ (driver) 3) Version of LabVIEW 4) ...

generating random generator in a shell script
Dear all, I want a functionality using which I can generate random generators within a shellscript. I discovered functions like rand, rand_r(), but nuttin seems to work out too well. I used these functions within a C program and called compiled binary to retrieve the numbers at regular interval. This is my C file: #include <stdio.h> #include <stdlib.h> void main() { int j=10000; srand(10000); j=rand(); printf("%d\n",j); } I also tried rand_r(&i), where i was set to ne number. THe problems am facing are: 1. I have a maximum limit set upto which I can retrieve the r...

-extract 120X9 matrix from 1000X9 matrix randomly
Hello all, Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix. How can i do that? Sincerely, "muk " wrote in message > Hello all, > > Now, i have 1000X9 matrix which is quality parameters. And, i want to extract 120X9 matris randomly from 1000x9 matrix. > How can i do that? > > Sincerely, help rand Example: A = rand(1000,9); rows = ceil(120*rand(120,1)); cols = ceil(9*rand(9,1)); randMat = A(rows,cols); Oleg muk wrote: > Now, i have 1000X9 matrix which is quality parameters. And, ...

Re: Random number generation
(Note: I didn't participate in the first discussion, as far as I recall, so if I am missing some key background piece, this could be irrelevant to the discussion.) It's precisely the same because the seed (8385) generates this string: ..91 ..19 ..52 ..05 ..63 ..39 ..88 ..24 ..52 ..16 big,little, big, little, big, little, big, little, big, little Change it to something else (83885, say) and it's no longer in that order. Furthermore, if 8385 ends up being a legitimate random number seed for (whatever your purposes are), and is selected in an effectively random way [say, the day t...

Pseudo-random number generator not random in first run
Hi, I am working on a project for my AI class that involves genetic programming. It basically takes a specific list (of rules) and applies random mutations until it reaches a final state. I wrote the whole thing pretty quickly thanks to LISP, but a peculiar point caught my attention. When I first run clisp from the command-line using \$ clisp -i main.lsp a function main (similar to C) is executed, and prints an integer (the number of iterations it took the system to reach the goal state). Yet, the first time the interpretor is executed, it always prints the same number! But when I rerun (mai...

how to generate a gaussian distribution using random number generator
i want to generate 50,000 samples according to the gaussian distribution using random number generator where sigma=1 and mean=0 X is a normally distributed random variable N(0,?2). mean=0 variance=1 I did this R=randn(50000) i got an error Error using randn Out of memory. Type HELP MEMORY for your options. and if i am using R=randn(50000,1); hist(R) this generated a distribution but the y label was ranging from -4 to 5. I want it from -infinity to +infinity how to do that? On 7/3/2013 6:52 PM, faraz.a wrote: > i want to generate 50,000 samples according to the ...