How to use eval in parfor
23 次查看(过去 30 天)
显示 更早的评论
Hello, all:
I got a problem in using Parfor. Matlab says eval can not be used for parfor. Would you please help me see how I can change this format so that it can works?
Additionally, would you please tell me if a structure also can not be used in parfor? The following commands works fine if I use for but when I used parfor, I got errors. Thank you very much
for i=1:5
parfor ii=1:10
eval(['A.B',num2str(i),'.C(ii,1) = ii;'])
end
end
0 个评论
回答(2 个)
Steven Lord
2017-2-22
I'd probably create a sliced output variable C inside the parfor loop and assign that variable to a field of your struct outside the parfor statement as shown in "Slicing structure fields" on this documentation page. Dynamic field names may be of use to you in this outer assignment step.
0 个评论
Stephen23
2017-2-22
编辑:Stephen23
2017-2-22
N = 10;
A(5).C = [];
for m = 1:5
V = NaN(1,N);
parfor n = 1:N
V(n) = m^n;
end
A(m).C = V;
end
This allows you to access the data very simply, e.g.:
>> vertcat(A.C)
ans =
Columns 1 through 7
1 1 1 1 1 1 1
2 4 8 16 32 64 128
3 9 27 81 243 729 2187
4 16 64 256 1024 4096 16384
5 25 125 625 3125 15625 78125
Columns 8 through 10
1 1 1
256 512 1024
6561 19683 59049
65536 262144 1048576
390625 1953125 9765625
and will be much easier to work with than nested structures.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!