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 (mxn)

Subject: Generate Random Matrix (mxn)

From: Vassilios Vassiliadis

Date: 2 Dec, 2010 07:18:04

Message: 1 of 8

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

Subject: Generate Random Matrix (mxn)

From: Walter Roberson

Date: 2 Dec, 2010 08:23:50

Message: 2 of 8

On 02/12/10 1:18 AM, Vassilios Vassiliadis wrote:

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

[a,b] = sort(rand(1000,49));
b = b(:,1:10);

> b. Matrix must have unique rows (non-repeatable rows) (less significant
> restriction).

It is low probability that the rows will not be unique, but you can
check via

size(unique(b,'rows'),1) ~= size(b,1)

Subject: Generate Random Matrix (mxn)

From: Vassilios Vassiliadis

Date: 2 Dec, 2010 11:09:06

Message: 3 of 8

Walter Roberson <roberson@hushmail.com> wrote in message <rIIJo.46667$qg3.38766@newsfe14.iad>...
> On 02/12/10 1:18 AM, Vassilios Vassiliadis wrote:
>
> > 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!!!)
>
> [a,b] = sort(rand(1000,49));
> b = b(:,1:10);
>
> > b. Matrix must have unique rows (non-repeatable rows) (less significant
> > restriction).
>
> It is low probability that the rows will not be unique, but you can
> check via
>
> size(unique(b,'rows'),1) ~= size(b,1)

Thanx,
however I forgot to mention that all randomly-generated numbers should be integer (and not floating)...

Sorry for the inconvinience...

Subject: Generate Random Matrix (mxn)

From: Walter Roberson

Date: 2 Dec, 2010 17:05:04

Message: 4 of 8

On 02/12/10 5:09 AM, Vassilios Vassiliadis wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <rIIJo.46667$qg3.38766@newsfe14.iad>...
>> On 02/12/10 1:18 AM, Vassilios Vassiliadis wrote:
>>
>> > 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!!!)
>>
>> [a,b] = sort(rand(1000,49));
>> b = b(:,1:10);
>>
>> > b. Matrix must have unique rows (non-repeatable rows) (less significant
>> > restriction).
>>
>> It is low probability that the rows will not be unique, but you can
>> check via
>>
>> size(unique(b,'rows'),1) ~= size(b,1)
>
> Thanx,
> however I forgot to mention that all randomly-generated numbers should
> be integer (and not floating)...

Did you look at what b contains?

Subject: Generate Random Matrix (mxn)

From: Vassilios Vassiliadis

Date: 2 Dec, 2010 18:01:05

Message: 5 of 8

Walter Roberson <roberson@hushmail.com> wrote in message <4lQJo.50899$3f.46097@newsfe12.iad>...
> On 02/12/10 5:09 AM, Vassilios Vassiliadis wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
> > <rIIJo.46667$qg3.38766@newsfe14.iad>...
> >> On 02/12/10 1:18 AM, Vassilios Vassiliadis wrote:
> >>
> >> > 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!!!)
> >>
> >> [a,b] = sort(rand(1000,49));
> >> b = b(:,1:10);
> >>
> >> > b. Matrix must have unique rows (non-repeatable rows) (less significant
> >> > restriction).
> >>
> >> It is low probability that the rows will not be unique, but you can
> >> check via
> >>
> >> size(unique(b,'rows'),1) ~= size(b,1)
> >
> > Thanx,
> > however I forgot to mention that all randomly-generated numbers should
> > be integer (and not floating)...
>
> Did you look at what b contains?

Hello,
thanx for the assist.However,b is a (1000x10) matrix with random numbers from the interval [1 1000] and not [1 49]...

Subject: Generate Random Matrix (mxn)

From: Sean de

Date: 2 Dec, 2010 18:12:04

Message: 6 of 8

"Vassilios Vassiliadis" <v.vassiliadis@fme.aegean.gr> wrote in message <id8mt1$n9g$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <4lQJo.50899$3f.46097@newsfe12.iad>...
> > On 02/12/10 5:09 AM, Vassilios Vassiliadis wrote:
> > > Walter Roberson <roberson@hushmail.com> wrote in message
> > > <rIIJo.46667$qg3.38766@newsfe14.iad>...
> > >> On 02/12/10 1:18 AM, Vassilios Vassiliadis wrote:
> > >>
> > >> > 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!!!)
> > >>
> > >> [a,b] = sort(rand(1000,49));
> > >> b = b(:,1:10);
> > >>
> > >> > b. Matrix must have unique rows (non-repeatable rows) (less significant
> > >> > restriction).
> > >>
> > >> It is low probability that the rows will not be unique, but you can
> > >> check via
> > >>
> > >> size(unique(b,'rows'),1) ~= size(b,1)
> > >
> > > Thanx,
> > > however I forgot to mention that all randomly-generated numbers should
> > > be integer (and not floating)...
> >
> > Did you look at what b contains?
>
> Hello,
> thanx for the assist.However,b is a (1000x10) matrix with random numbers from the interval [1 1000] and not [1 49]...

yes there is an error in the sort line. It's sorting along the first dimension (rows) and not the second. To fix this:
[a,b] = sort(rand(1000,49),2);

Subject: Generate Random Matrix (mxn)

From: Walter Roberson

Date: 2 Dec, 2010 18:17:50

Message: 7 of 8

On 02/12/10 12:01 PM, Vassilios Vassiliadis wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <4lQJo.50899$3f.46097@newsfe12.iad>...
>> On 02/12/10 5:09 AM, Vassilios Vassiliadis wrote:
>> > Walter Roberson <roberson@hushmail.com> wrote in message
>> > <rIIJo.46667$qg3.38766@newsfe14.iad>...
>> >> On 02/12/10 1:18 AM, Vassilios Vassiliadis wrote:
>> >>
>> >> > 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!!!)
>> >>
>> >> [a,b] = sort(rand(1000,49));
>> >> b = b(:,1:10);

> thanx for the assist.However,b is a (1000x10) matrix with random numbers
> from the interval [1 1000] and not [1 49]...

Sorry, make it

[a,b] = sort(rand(1000,49),2);
b = b(:,1:10);

Subject: Generate Random Matrix (mxn)

From: Vassilios Vassiliadis

Date: 3 Dec, 2010 07:08:04

Message: 8 of 8

Walter Roberson <roberson@hushmail.com> wrote in message <jpRJo.49653$NM2.8061@newsfe22.iad>...
> On 02/12/10 12:01 PM, Vassilios Vassiliadis wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
> > <4lQJo.50899$3f.46097@newsfe12.iad>...
> >> On 02/12/10 5:09 AM, Vassilios Vassiliadis wrote:
> >> > Walter Roberson <roberson@hushmail.com> wrote in message
> >> > <rIIJo.46667$qg3.38766@newsfe14.iad>...
> >> >> On 02/12/10 1:18 AM, Vassilios Vassiliadis wrote:
> >> >>
> >> >> > 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!!!)
> >> >>
> >> >> [a,b] = sort(rand(1000,49));
> >> >> b = b(:,1:10);
>
> > thanx for the assist.However,b is a (1000x10) matrix with random numbers
> > from the interval [1 1000] and not [1 49]...
>
> Sorry, make it
>
> [a,b] = sort(rand(1000,49),2);
> b = b(:,1:10);

Thank you very much for the assist...It has been very helpful...

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