How to use parfor to make a new matrix form an existing one
1 次查看(过去 30 天)
显示 更早的评论
Hi. I have a matrix A which is used to generate a new matrix B within loops. I used parfor to do this but it is not working. Could you please someone let me know how I can modify the code to make use of parfor. The code is as follows:
n_c=8;
n_c1=n_c+1;
L=2*(n_c1);
h=size(A);
h1=h(1);
nn_1=n-1; nn_2=2*n-2;
s2=2*n;
L_1=L-2:
for a=0:1
for b=0:1
if n<=n_c1
B=zeros(h1,s2);
parfor i=1:h1
B(i,1:s2)=[a,A(i,1:nn_1),b,A(i,n:nn_2)];
end
else
B=zeros(h1,L);
parfor i=1:h1
B(i,1:L)=[a,A(i,1:n_c),b,A(i,n_c1:L-2)];
end
end
end
end
This code is inside a bigger loop with the counter n.
Thank you very much
0 个评论
回答(1 个)
Walter Roberson
2019-5-4
In both cases, you are assigning to an entire column of B. Skip the 1:s2 or 1:L indexing: they are confusing parfor. Just assign to B(i,:)
But remember your code is going to create a completely new B matrix for each iteration of a and b, so you might as well only do the last a and last b since you are overwriting all of B each time.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!