Thread Subject: parfor loop

Subject: parfor loop

From: Eric Anterrieu

Date: 1 Oct, 2009 14:33:03

Message: 1 of 4

Is there a trick for bypassing the constraint on array indexing in a parfor loop?

Exemple:
A=zeros(2*n);
for k=1:n
   W = complex number returned by a function using index k
   A(k) = real(W);
   A(k+n) = imag(W);
end
which does not work because of k+n indexing...

Do not ask me why I am working with complex number but use real valued matrices, it is another problem (shortly speaking: when solving y=Ax with both y and A complex valued but with real valued solution x, one whay to be sure to obtain a real valued solution is to proceed with the underlying real valued matrix A and y).

I have think of some solutions which do not satisfy me:
1)
a=zeros(n)+j*zeros(n);
for k=1:n
   W = complex number returned by a function using index k
   a(k) = W;
end
A = [real(a); imag(a)];
2)
a=zeros(n);
b=zeros(n);
for k=1:n
   W = complex number returned by a function using index k
   a(k) = real(W);
   b(k) = imag(W);
end
A = [a; b];

Subject: parfor loop

From: Edric M Ellis

Date: 1 Oct, 2009 15:15:58

Message: 2 of 4

"Eric Anterrieu" <eric.anterrieu@ast.obs-mip.fr> writes:

> Is there a trick for bypassing the constraint on array indexing in a parfor
> loop?

Unfortunately there is not.

> Exemple:
> A=zeros(2*n);
> for k=1:n
> W = complex number returned by a function using index k
> A(k) = real(W);
> A(k+n) = imag(W);
> end
> which does not work because of k+n indexing...

The only ways around this are broadly similar to the solutions that you came up
with - i.e. index something inside the loop using only "k", and then re-arrange
afterwards. Here's the variant I came up with:

A = zeros( N, 2 );
parfor k = 1:N
    W = complex( rand, rand );
    A( k, : ) = [ real( W ), imag( W ) ];
end
A = reshape( A, N * 2, 1 );

Cheers,

Edric.

Subject: parfor loop

From: Raymond Norris

Date: 1 Oct, 2009 20:01:03

Message: 3 of 4

Eric,

Your solutions produce matrix dimensions different than your current implementation. Going back to your original code, tell me if the following is true (for n=10):
1. You want A to be allocated as 20x20?
2. You're only modifying the first 20x1 elements in the for loop
3. The first 10x1 elements are the real part and the second 10x1 elements are the imaginary part.

Raymond

"Eric Anterrieu" <eric.anterrieu@ast.obs-mip.fr> wrote in message <ha2eiv$cam$1@fred.mathworks.com>...
> Is there a trick for bypassing the constraint on array indexing in a parfor loop?
>
> Exemple:
> A=zeros(2*n);
> for k=1:n
> W = complex number returned by a function using index k
> A(k) = real(W);
> A(k+n) = imag(W);
> end
> which does not work because of k+n indexing...
>
> Do not ask me why I am working with complex number but use real valued matrices, it is another problem (shortly speaking: when solving y=Ax with both y and A complex valued but with real valued solution x, one whay to be sure to obtain a real valued solution is to proceed with the underlying real valued matrix A and y).
>
> I have think of some solutions which do not satisfy me:
> 1)
> a=zeros(n)+j*zeros(n);
> for k=1:n
> W = complex number returned by a function using index k
> a(k) = W;
> end
> A = [real(a); imag(a)];
> 2)
> a=zeros(n);
> b=zeros(n);
> for k=1:n
> W = complex number returned by a function using index k
> a(k) = real(W);
> b(k) = imag(W);
> end
> A = [a; b];

Subject: parfor loop

From: Eric Anterrieu

Date: 5 Oct, 2009 08:34:01

Message: 4 of 4

Edric M Ellis <eellis@mathworks.com> wrote in message <ytweipnjgb5.fsf@uk-eellis-deb5-64.mathworks.co.uk>...
> "Eric Anterrieu" <eric.anterrieu@ast.obs-mip.fr> writes:
>
> > Is there a trick for bypassing the constraint on array indexing in a parfor
> > loop?
>
> Unfortunately there is not.
That was my fear, but I was expected a function/commmand I did no know!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
parfor Sprinceana 1 Oct, 2009 12:09:07
loop Sprinceana 1 Oct, 2009 12:09:07
rssFeed for this Thread

Contact us at files@mathworks.com