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:
Assign binary number to a matrix randomly with a constraint

Subject: Assign binary number to a matrix randomly with a constraint

From: Philippe Delaruelle

Date: 21 Mar, 2013 12:40:15

Message: 1 of 3

Hi,

I have the following question. I have a 192x100 matrix where the 192 stands for the periods and the 100 for the number of appliances. I want to assign for each row randomly a number of 1's with the constraint that the total number of 1's in that row should be equal to an in advance declared number (lets say 8). How do I do this?

Is is also possible that when an appliance (1 of the 100) has a 1 in a certain period (row), the next 8 periods (rows) it can't have a new 1 assigned and thus has to be 0?

Thanks in advance
I truly hope somebody can help me..

Subject: Assign binary number to a matrix randomly with a constraint

From: Alan_Weiss

Date: 21 Mar, 2013 15:56:39

Message: 2 of 3

On 3/21/2013 8:40 AM, Philippe Delaruelle wrote:
> Hi,
>
> I have the following question. I have a 192x100 matrix where the 192
> stands for the periods and the 100 for the number of appliances. I
> want to assign for each row randomly a number of 1's with the
> constraint that the total number of 1's in that row should be equal to
> an in advance declared number (lets say 8). How do I do this?
>
> Is is also possible that when an appliance (1 of the 100) has a 1 in a
> certain period (row), the next 8 periods (rows) it can't have a new 1
> assigned and thus has to be 0?
>
> Thanks in advance
> I truly hope somebody can help me..

For your first question, make a vector of 8 ones and 92 zeros, and then
use randperm:

x1 = [ones(1,8) zeros(1,92)];
x2 = x1(randperm(100))

For your second question, perhaps your ones should represent the string
'1 0 0 0 0 0 0 0 0'. But I do not know how that would help if you allow
a 1 towards the end, without so many 0s after it.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Assign binary number to a matrix randomly with a constraint

From: Bruno Luong

Date: 21 Mar, 2013 21:41:06

Message: 3 of 3

"Philippe Delaruelle" wrote in message <kiev3f$di6$1@newscl01ah.mathworks.com>...

> Is is also possible that when an appliance (1 of the 100) has a 1 in a certain period (row), the next 8 periods (rows) it can't have a new 1 assigned and thus has to be 0?

Do you mean that there does not exist two 1s at two consecutive rows and in the same column?

Bruno

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