# using variables in a parfor loop

4 views (last 30 days)

Show older comments

Hey, I am very new to the parallel computing and i got some trouble.

So basicly i have so example code very similar to this one:

a = (normrnd(1,1,[100000,1]))+5;

i=0;

while i<=10000

for j=1:length(a)

a(j)=a(j)+1/a(j);

end

i=i+1;

end

Inside the nested for loop are many more alculations which takes a huge amount of time, so i basically want to cut a into as much pieces as i have physical cores, so in this case 4 *20000 and then do the same calculations of thos 20000 arrays parrallel.

But I direclty get some error cause i cannot use my input variable from outside the parfor loop because of classification (Code without the outside while loop)

parfor i=1:cores

v=d(:,i);

v=v(v>0);

lim=size(v,1);

for j=1:lim

v(j)=v(j)+1/v(j);

end

d(1:length(v),i)=v;

end

Shouldnt my input d (20000x4 array) not work as an broadcast variable as defined in https://de.mathworks.com/help/parallel-computing/troubleshoot-variables-in-parfor-loops.html ?

Or am I missanderstanding something very basic in parallel computing?

Many thanks in advance

Best regards

##### 0 Comments

### Accepted Answer

Daniel M
on 9 Nov 2019

##### 2 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!