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:
Generating a subarray of random numbers

Subject: Generating a subarray of random numbers

From: Paul

Date: 19 May, 2011 01:51:02

Message: 1 of 5

Is it possible to generate a subarray of random numbers without generating the whole array and then indexing into it? For example:

Z = randn(1000,1000);
for ii = 1:10
for jj = 1:10
Y{ii,jj} = Z(100:100+ii,200:200+jj);
end
end

Is it possible to generate the relatively few elements of Y without having to generate all of the elements of Z, given the state of the random number stream upon entry into the first for loop?

Subject: Generating a subarray of random numbers

From: ImageAnalyst

Date: 19 May, 2011 02:06:52

Message: 2 of 5

If Y is truly a subarray, that is, it's contained somewhere inside Z,
then of course Z must already exist.

But your Y is not simply a single subarray - what you made is a cell
array, which is a list of 100 subarrays all of different sizes
(shapes). You have arrays of sizes 2x2, 2x3, 2x4, etc. up to 11x11 -
one hundred and twenty one of them. Are you sure that is what you
want?

Subject: Generating a subarray of random numbers

From: Roger Stafford

Date: 19 May, 2011 02:49:03

Message: 3 of 5

"Paul" wrote in message <ir1t26$909$1@newscl01ah.mathworks.com>...
> Is it possible to generate a subarray of random numbers without generating the whole array and then indexing into it? For example:
>
> Z = randn(1000,1000);
> for ii = 1:10
> for jj = 1:10
> Y{ii,jj} = Z(100:100+ii,200:200+jj);
> end
> end
>
> Is it possible to generate the relatively few elements of Y without having to generate all of the elements of Z, given the state of the random number stream upon entry into the first for loop?
- - - - - - - - - - - -
  What you ask would be extremely difficult to achieve, Paul. You would have to understand Mathworks' algorithm in generating random numbers very thoroughly so as to be able to simulate the large jumps in the seed and "shift register" values needed to get to the subarray you are interested in from the initial seed values you have, and to do so significantly faster than do 'randn' calls themselves to make it useful. Also it is possible that the sequencing would not be consecutive through the given subarray, so there would then have to be many such simulated "jumps". That's a project I wouldn't want to undertake, even if I had Mathworks' complete cooperation.

Roger Stafford

Subject: Generating a subarray of random numbers

From: Paul

Date: 21 May, 2011 00:09:04

Message: 4 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c778b909-f71d-45ab-965f-54b96a5128ce@t19g2000yql.googlegroups.com>...
> If Y is truly a subarray, that is, it's contained somewhere inside Z,
> then of course Z must already exist.
>
> But your Y is not simply a single subarray - what you made is a cell
> array, which is a list of 100 subarrays all of different sizes
> (shapes). You have arrays of sizes 2x2, 2x3, 2x4, etc. up to 11x11 -
> one hundred and twenty one of them. Are you sure that is what you
> want?

I'm pretty sure that this is exactly what I want. Basically, the application is that Z represents some random conditions specified at the beginning of a simulation and the loops represent the simulation iteration. Before the simulation, one doesn't know which subarrays are going to be needed, just that some will be needed and that it's important that overlaps between the subarrays are consistent across the iterations.
 

Subject: Generating a subarray of random numbers

From: Paul

Date: 21 May, 2011 00:23:04

Message: 5 of 5

"Roger Stafford" wrote in message <ir20ev$grt$1@newscl01ah.mathworks.com>...
> "Paul" wrote in message <ir1t26$909$1@newscl01ah.mathworks.com>...
> > Is it possible to generate a subarray of random numbers without generating the whole array and then indexing into it? For example:
> >
> > Z = randn(1000,1000);
> > for ii = 1:10
> > for jj = 1:10
> > Y{ii,jj} = Z(100:100+ii,200:200+jj);
> > end
> > end
> >
> > Is it possible to generate the relatively few elements of Y without having to generate all of the elements of Z, given the state of the random number stream upon entry into the first for loop?
> - - - - - - - - - - - -
> What you ask would be extremely difficult to achieve, Paul. You would have to understand Mathworks' algorithm in generating random numbers very thoroughly so as to be able to simulate the large jumps in the seed and "shift register" values needed to get to the subarray you are interested in from the initial seed values you have, and to do so significantly faster than do 'randn' calls themselves to make it useful. Also it is possible that the sequencing would not be consecutive through the given subarray, so there would then have to be many such simulated "jumps". That's a project I wouldn't want to undertake, even if I had Mathworks' complete cooperation.
>
> Roger Stafford

Roger,

I wouldn't want to undertake that either. Instead, I was hoping that there was some randstream functionality that I wasn't aware of. Something like

randn(stream,[m n],k,j)

which would mean that for the specified stream, return to me the matrix subarray, as indicated by the k,j indices, of the m x n matrix that randn would normally generate.

I couldn't find such explicit functionality in the documentation, but was hoping that maybe I missed something or perhaps there was another approach to accomplish this without duplicating what's already being done in the random number generator.

Thanks,
Paul

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