Parfor+for+if+if How to calculate variable form last if

See my code
parfor i = 1:n
for j=1:m
if some_condition(i)
if some_condition(i)
t=do_something(i);
b = [b;t];
end
end
end...
end
I want all b values to get out from the par

 Accepted Answer

b = [];
parfor i = 1:n
bt = [];
for j=1:m
if some_condition(i)
if some_condition(i)
t=do_something(i);
bt = [bt;t];
end
end
end...
b = [b, bt];
end

5 Comments

Doesn't this rely on bt having the same size at each iteration?
No, https://www.mathworks.com/help/parallel-computing/reduction-variable.html does not place that restriction on using appending for reduction variables.
But if I substitute for some_condition and do_something in your code, and define the numbers of iterations, I get the error below. Any idea what's causing this?
m = 7;
n = 3;
b = [];
parfor i = 1:n
bt = [];
for j=1:m
if rand > 0.5
if rand > 0.5
t=i;
bt = [bt;t];
end
end
end
b = [b, bt];
end
Dimensions of arrays being concatenated are not consistent.
m = 7;
n = 3;
b = [];
parfor i = 1:n
bt = [];
for j=1:m
if rand > 0.5
if rand > 0.5
t=i;
bt = [bt;t];
end
end
end
b = [b; bt];
end
b
b = 7x1
1 1 2 2 3 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ah, it needed to be [b; bt] rather than [b, bt]. Thanks!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!