MATLAB Answers

# Problems encountered in Parallel Processing

4 views (last 30 days)
Benson Gou on 30 Aug 2019
Commented: Benson Gou on 30 Aug 2019
Dear All,
I tried to use parfor to run my code which is given as follows:
function [A,B] = temp_test(x)
a = [1:2*x]; b = [1:x];
A = zeros(1,x); B = zeros(x,1);
parfor i = 1 : x
j = x + 1;
c = func1(a(j),b(i));
A(i) = c;
B(i) = a(j) + b(i);
end
function [d] = func1(e,f)
d = e*f;
end
end
But I was told I can not call func1 like this because it is a nested function. Would anybody tell me how I can fix this problem but I still want to keep the func1 as it is?
Thanks a lot.
Benson
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Matt J on 30 Aug 2019
Is there any reason not to simply make func1 a local function?
function [A,B] = temp_test(x)
a = [1:2*x]; b = [1:x];
A = zeros(1,x); B = zeros(x,1);
parfor i = 1 : x
j = x + 1;
c = func1(a(j),b(i));
A(i) = c;
B(i) = a(j) + b(i);
end
end
function [d] = func1(e,f)
d = e*f;
end
##### 5 CommentsShowHide 4 older comments
Benson Gou on 30 Aug 2019
Dear Matt,
I am very sorry I did not clearlly read your answer. I tried your changed code and it works very well.
Thanks a lot and have a good weekend!
Benson

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!