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

1 view (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 4 May 2019
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!