Nested for loop in parfor, indexing

1 Ansicht (letzte 30 Tage)
alex
alex am 14 Mär. 2013
Hello!
I'm running Monte Carlo simulation in Matlab 2012a, the following code returns an error
if matlabpool('size') == 0
matlabpool open
end
parfor i=1:M
for j=2:N
x(i,j)=x1(i,j-1)+sqrt(x(i,j-1))
y(i,j)=y(i,j-1)+sqrt(y(i,j-1))
z(i,j)=z(i,j-1)+x(i,j-1)+y(i,j-1)
end
end
matlabpool close
(Definitions of x ,y, z are simplified but the main idea remains) As I understand it is impossible to use indexing this way in a nested 'for' 'cos Matlab cannot check if array elements are independent. But in my situation it's clear that index i (different MC paths are independent) is remaining unchanged in for loop, only j (time steps) is being changed. Could you please give me a piece of advice with this issue?
Thanks in advance, Alex.
P.S. the error is obvious for such cases: The variable x in a parfor cannot be classified.

Akzeptierte Antwort

ChristianW
ChristianW am 14 Mär. 2013
You could use a seperated variable for the inner loop. On the example of x:
X = zeros(M,N); %preallocate
parfor i=1:M
x1 = zeros(1,N); %preallocate
x1(1) = x(i,1);
for j=2:N
x1(j)=x1(j-1)+sqrt(x1(j-1));
end
X(i,:) = x1;
end
But usually this should be much faster:
X1 = [x zeros(M,N-1)]; % first column and preallocate
for k=2:N
X1(:,k)=X1(:,k-1)+sqrt(X1(:,k-1));
end
  2 Kommentare
alex
alex am 15 Mär. 2013
Big thanks for you answer, Christian, the first part of your code works great! But still I cannot implement the second part, maybe it's really impossible to do this way in my case. The issues is that in real loop is a bit more complicated: I need to have a random vector and to apply each element of it to my X1 row in the way
X = zeros(M,N); %preallocate
parfor i=1:M
eps1=randn(1,N)
x1 = zeros(1,N); %preallocate
x1(1) = x(i,1);
for j=2:N
x1(j)=x1(j-1)+eps1(j-1)*sqrt(x1(j-1))
end
X(i,:) = x1;
end
The upper code works fine, but in order to follow your advice I tried the following code and clearly it didn't work
eps1=randn(M,N);
for j=2:N
x1(:,j)=x1(:,j-1)+eps1(:,j-1)*sqrt(x1(:,j-1))
end
The point is that the first index in eps isn't changin - so I have one and the same eps1 for all i indexes (1 to M). I also tried to put eps into for
for j=2:N
eps1=randn(1,N);
x1(:,j)=x1(:,j-1)+eps1(:,j-1)*sqrt(x1(:,j-1))
end
This construction neither worked. Am I using this loop wrong or it is impossible to make such a short construction?
Thanks in advance!
ChristianW
ChristianW am 15 Mär. 2013
Both work. The first one is faster. You have some errors in it. You need to give him the first column.
x2 = [x zeros(M,N-1)]; % first column and preallocate
eps1 = randn(M,N);
for j = 2:N
x2(:,j) = x2(:,j-1)+eps1(:,j-1).*sqrt(x2(:,j-1));
end
x3 = [x zeros(M,N-1)]; % first column and preallocate
for j=2:N
eps1 = randn(M,1);
x3(:,j) = x3(:,j-1)+eps1.*sqrt(x3(:,j-1));
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by