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
Related
I am trying to generate a heteroskedastic error term in MATLAB.
I have found a good link to help here, the problem I am having is replicating this in MATLAB.
Here is my attempt:
n = [(1:100) (1:100)]';
sigma2 = n.^(1.3);
t = size(n,1);
for i = 1:200
eps(i) = normrnd(0, sqrt(sigma2(1)));
end
eps = eps'
h = archtest(eps)
However, the test for hetero indicates I still do not have heteroskedastic data, can anyone see where I am going wrong.
Looks like you're keeping sigma2 fixed in the first value inside the loop.
Replace
eps(i) = normrnd(0, sqrt(sigma2(1)));
with
eps(i) = normrnd(0, sqrt(sigma2(i)));
I have to perform a copulapdf analysis and i need to create 8 different figures starting from varying nu (in this code called nuvar) from 1 to 8. I'm new in Matlab. I tried to write this for cycle but it doesn't works. Can someone help me please?
for nuvar= 1:1:8
for numvar1= 1:1:8
r=0.5;
nu=1;
u = linspace(0,1,20);
[u1,u2] = meshgrid(u,u);
rho1 = copulaparam('t',r,nu);
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
end
I also tried to correct the script in this way:
r=0.5;
nu=1;
u = linspace(0,1,20);
rho1 = copulaparam('t',r,nu);
[u1,u2] = meshgrid(u,u);
for numvar1= 1:1:8
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
I have the same error "Subscripted assignment dimension mismatch".
I am trying to set up a parfor nested loop in MatLab R2016a as below.
N = size(A,1);
M = size(v,1);
in = zeros(N*M,1);
parfor i=1:N
for j=1:M
k = (i-1)*M+j;
if sqrt(sum((A(i,:)-v(j,:)).^2))<=tol
in(k) = i;
end
end
end
However, I am getting the following error Valid indices for 'in' are restricted in PARFOR loops. Is there some way I can correct this because both arrays A and v are considerably large, over 40,000 rows for A and 8,000 v? The variable tol is 0.0959.
The problem is that MATLAB doesn't recognize that the variable k is slicing the matrix in correctly. The solution should be to index in using i and j separately:
N = size(A,1);
M = size(v,1);
in = zeros(M,N);
parfor i=1:N
for j=1:M
if sqrt(sum((A(i,:)-v(j,:)).^2))<=tol
in(j,i) = i;
end
end
end
in = in(:); % reshape to a column vector, as the output in the question's code
The other alternative, but it requires more intermediate memory, is to compute this without a loop at all:
A = reshape(A,1,N,[]);
v = reshape(v,M,1,[]);
in = sum(bsxfun(#minus,A,v).^2,3) < tol*tol;
in = in(:);
(Or something similar to that, I have not run this code... Please let me know, or fix the post, if there is a typo or other mistake.)
N = size(A,1);
M = size(v,1);
in = cell(N,1);
parfor i=1:N
s=v;
p=zeros(1:M,1);
for j=1:M
k = (i-1)*M+j;
if sqrt(sum((A(i,:)-s(j,:)).^2))<=tol
p(k) = i;
end
end
in{i}=single(p);
end
in=cell2mat(in);
in=reshape(in,[N*M,1]);
sometime matlab doesnt recognize a variable in parfor loop as "sliced variable"
, a sliced variable is a variable that has a reference out of parfor loop and each of its element only accessed by a single worker (in parfor parallel workers)
so you could use a temporary variable and collect results after the parfor loop,
NOTE 1: It is better to vectorise code in the older versions because loops didn't use to be as good as they are now in R2017 referring to (this).
NOTE 2: if most elements of "in" are zero try using "sparse matrix" which could save a lot of memory;
I'm trying to use arrayfun() to map a function over a cell array. The following is happening:
>> arrayfun(solveFunc, equArray)
Error using arrayfun
First input must be a function handle.
Error in solve>genGuess (line 33)
funcVals = abs(arrayfun(inFunc, xValues));
Error in solve (line 8)
x = genGuess(inFunc, varargin{1}, varargin{2});
Error in makeSolveFunc>#(func)solve(func,start,stop) (line 3)
sFunc = #(func) solve(func, start, stop);
But, the first input IS a function handle. Also... if I manually apply the function to each element of the provided cell array, everything works fine:
>> solveFunc(equArray{1})
ans =
4.7335
>> solveFunc(equArray{2})
ans =
4.7356
Does anyone know why this would be happening? I assumed that if I could manually apply the function to each element of my array, and the return type of the function was consistent and one of the allowed types (you can't for example have arrayfun return an array of function handles... I already tried doing that), it should work. Perhaps that is not the only requirement.
Here is some code that generates this error:
solve.m
function solution = solve(inFunc, start, stop)
%SOLVE solve an equation using Newton's Method
x = genGuess(inFunc, start, stop);
for i = 1:100
m = getSlope(inFunc, x);
x = (m*x - inFunc(x))/m;
end
solution = x;
end
function slope = getSlope(inFunc, x)
%SLOPE calculate the slope at a given point
inc = 1e-5;
if x ~= 0
inc = inc * x;
end
slope = (inFunc(x + inc) - inFunc(x - inc))/(2*inc);
end
function guess = genGuess(inFunc, start, stop)
%GENGUESS get an initial guess to the solution
xValues = linspace(start, stop, 101);
funcVals = abs(arrayfun(inFunc, xValues));
[~, minIndex] = min(funcVals);
guess = xValues(minIndex);
end
charEqu.m
function equ = charEqu(a)
%CHAREQU create a KP model characteristic equation with provided p
equ = #(x) x + a;
end
makeSolveFunc.m
function sFunc = makeSolveFunc(start, stop)
%MAKESOLVEFUNC return a function that solves an equation
sFunc = #(func) solve(func, start, stop);
end
test.m
pArray = 1:5;
equArray = cell(1,arrayLen);
for i = 1:5
equArray{i} = charEqu(pArray(i));
end
solveFunc = makeSolveFunc(1.1*pi, 2*pi);
alphaAArray = arrayfun(solveFunc, equArray);
I have narrowed down the error to something in genGuess(). For some reason, in the line funcVals = abs(arrayfun(inFunc, xValues)); the variable inFunc is a 1x1 cell array containing a function handle. I have no idea why that would be the case. I traced this back to the anonymous function call #(func) solve(func, start, stop); in the makeSolveFunc() function. There it is still a 1x1 cell array containing a function handle. I'm not really sure where that cell array is coming from as that function is getting called from arrayfun().
Background information on what I'm trying to do in case someone wants to suggest a better way:
I'm trying to solve equations using Newton's method. I have written a function that can solve an equation given an initial guess range. This function is the solve() function you can see in the first error message. It takes a function, and the guess range and returns a function that I'm calling solveFunc(). solveFunc() takes a function and solves it using the initial guess range previously provided.
Maybe I'm just too used to functional programming and should just use a loop.
If the arguments passed to the function handle are contents of elements of a cell array, you need to use cellfun instead of arrayfun:
cellfun(solveFunc, equArray)
This is equivalent to
for i=1:length(equArray)
out(i) = solveFunc(equArray{i});
end
since solveFunc is already a function handle.
Check where the error comes from. This line causes the error:
funcVals = abs(arrayfun(inFunc, xValues));
The first input argument is a 1x1 cell containing one function handle. This is caused because equArray is a cell, thus use cellfun as Jonas already mentioned:
pArray = 1:5;
equArray = cell(1,arrayLen);
for i = 1:5
equArray{i} = charEqu(pArray(i));
end
solveFunc = makeSolveFunc(1.1*pi, 2*pi);
alphaAArray = cellfun(solveFunc, equArray);
I am trying to parallize two of my for-loops and run it on a remote cluster.
I am using matlabpool open local 12 at the beginning with matlabpool close at the end. The problem I am running into is that my parfor-loop cannot use my matric properly and I am not sure how I would rewrite it so that it works.
H = hadamard(n);
H = [H;-H];
P = setdiff(P,H,'rows');
[r,c] = size(P);
A = zeros(n,r);
parfor i=1:r
for j=1:n
d = P(i,:) + H(j,:);
A(j,i) = sum(d(:) ~= 0);
end
end
and:
u2Had = cell(2,r);
parfor i =1:r
u2Had{1,i} = min(A(:,i));
MinHadIndex = find(A(:,i) == u2Had{1,i});
u2Had{2,i} = MinHadIndex;
end
Those are the two segments of the code I am trying to parallize. Any help is much appreciated and if I need to add anymore information please ask.
I don't know what your problem is in the first part as it works fine (perhaps if you defined P better)
regarding the second part, you can only send information to and from parloops in narrow cases.
Here change your code to the following:
u2HadT = cell(1,r);
parfor i =1:r
temp = min(A(:,i));
MinHadIndex = find(A(:,i) == temp);
u2HadT{i} = {temp;MinHadIndex};
end
u2Had = cell(2,r);
for i =1:r
u2Had{1,i} = u2HadT{i}(1);
u2Had{2,i} = u2HadT{i}(2);
end