Just my two cents: As @mmumbos already explained, what you're trying to achieve is not possible this way, but certain workarounds can still apply.
Let's assume you need to implement something like the following (which is the permutation of a linear transformation of matrix A
):
n=10;
A=rand(n,n);
B=zeros(n,n);
parfor i=1:n,
j=(i<=5)*(2*(i-1)+1)+(i>5)*(2*(i-5));
B(j,i) = my_function(A(i,:));
end
Try instead the following:
parfor i=1:n,
B_temp(i,:) = my_function(A(i,:));
end
Then, you don't really need to construct matrix B
; you can access it using B_temp
using an ''index table'' which is simply constructed as follows:
J=zeros(n,1);
parfor i=1:n,
J(i) = (i<=5)*(2*(i-1)+1)+(i>5)*(2*(i-5));
end
Then B(i)
is accessed via B_temp(J(i))
.
Revisiting your last (counter)example, let's see how you can work it around:
n=4;
diag_A = zeros(n,1);
parfor i=1:n,
diag_A(i)=3; % or any function of i
end
Then, whenever you need to access the ''i''-th diagonal element of A
, you access instead diag_A(i)
. For such cases it is convenient to create a function along the following lines:
function a = access_matrix(A, diag_A, i, j)
if (i!=j),
a = A(i,j);
else
a = diag_A(i);
end
end
Until MATLAB improves the implementation of parfor
, such workarounds will (unfortunately) be necessary in lots of cases.