Hello All,
I am trying to make use of parfor, but I keep getting the following error "parfor cannot run due to the way the variable 'data' is used." Any thoughts?
Thanks in advance
parfor iel=1:tne
start=(iel-1)*576+1;
end1=576*iel;
data(start:end1)=data2(iel,:);
end

 採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 1 日

1 投票

parfor iel=1:tne
data((iel-1)*576+1:576*iel)=data2(iel,:);
end
That is, MATLAB can analyze literal indices in ways that it cannot do when those indices are computed.
However, unless you have good reason not to, you should simply create data as a 576 x something array and without using parfor,
data(:, 1:tne) = data2(:, 1:tne) .';
... especially if you can manage to compact it down to just
data = data2 .';
After the copying has been done with 2D indices, then it is very fast to
data = reshape(data, 1, []);

5 件のコメント

Diab Abueidda
Diab Abueidda 2019 年 2 月 1 日
Many thanks Walter for the answers!
I could not follow what you have mentioned. May you elaborate more?
Thanks
Walter Roberson
Walter Roberson 2019 年 2 月 1 日
What is size(data) before the parfor loop? What is size(data2) ? What is tne ?
Diab Abueidda
Diab Abueidda 2019 年 2 月 1 日
Before the parfor loop data is initialized as
data=zeros(tne*576,1);
tne is the total number of elements in the finite element analysis. What I am trying to do is construct the global stiffness matrix as a sparse matrix.
parfor iel=1:tne
data((iel-1)*576+1:576*iel)=data2(iel,:);
end
K=sparse(row,col,data,ndof,ndof)
size of data2 is tnex576
Walter Roberson
Walter Roberson 2019 年 2 月 1 日
Then
data = reshape(data2.', 1, []);
with no loop.
Diab Abueidda
Diab Abueidda 2019 年 2 月 1 日
Oops! You are right!
Thanks a lot

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by