How to use parfor to make a new matrix form an existing one

조회 수: 1 (최근 30일)
Davoud
Davoud 2019년 5월 4일
답변: Walter Roberson 2019년 5월 4일
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

답변 (1개)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by