I got trouble setting up a parfor loop in matlab. I know its rather easy but I am a bit stuck here thats why I would appreciate all help.
I have tried the following
valuesforM = zeros(901,100);
valuesforOPratio = zeros(100,1);
counter=1;
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','B2:B10000');
parfor M = 100:1000;
counter=counter+1
for OPratio = 1:100;
NPVtotal = cut_off_optimisation(M,OPratio,x,y,z);
valuesforOPratio(OPratio)=NPVtotal;
end
valuesforM(M-99,:) = valuesforOPratio;
end
And i get the following error:
Error using senitivity_script_10000_steps (line 10)
Error: The variable valuesforOPratio in a parfor cannot be classified.
How can i fix this ? Thanks a lot.
EDIT
following the commments advice I tried the following:
valuesforM = zeros(901,100);
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','C2:C10000');
parfor M = 100:1000;
NPVtotal = cut_off_optimisation(M,1:100,x,y,z);
valuesforM(M-99,:) = NPVtotal;
end
which gives the following error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in parforscript (line 8)
parfor M = 100:1000;
Any idea how to solve either of the both problems?
As you said in the comments, since counter is not required I have removed it and also counter won't work like that because different iterations will be running in a non sequential way and trying to update the same variable. Which is not allowed.
Next, you have to do valuesforOPratio = zeros(1,100)
inside parfor loop
because if you put it outside the loop then each iteration will be trying to access the same variable. This is not allowed. So that is why you are getting that error. When you put it inside, each iteration will be accessing a locally created variable. That is also the reason why you don't find it in base workspace. Here is the corrected code.
valuesforM = zeros(901,100);
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','B2:B10000');
parfor M = 100:1000;
valuesforOPratio = zeros(1,100);
for OPratio = 1:100;
NPVtotal = cut_off_optimisation(M,OPratio,x,y,z);
valuesforOPratio(OPratio)=NPVtotal;
end
valuesforM(M-99,:) = valuesforOPratio;
end