Alternative ways to slice/manage data using PARFOR

7 次查看(过去 30 天)
Hello,
I am a beginner MATLAB user, hence probably my question seems simple but I have spent quite a bit of time trying to find solutions for this.
I essentially have a large number of variables, or arrays of data (over 130) that are populated by data generated in a parfor loop (I am keen to use parfor because the script will run through quite large amounts of data, and is significantly more efficient).
As I have said it is a large number of arrays, and the way I am creating the emtpty arrays, and then populating them and managing them later in the scrip is just very line-consuming and does not look good to me.
The script (this is a very simplified example to illustrate the problem) looks like this:
Data = rand(1,1000);
A = nan(1,1000);
B = nan(1,1000);
C = nan(1,1000);
D = nan(1,1000);
E = nan(1,1000);
% (in my cript there is 130 variables )
parfor i = 1:1000
a = (Data(1,i) * 526^3)/5;
b = (Data(1,i) * 255^2)/6;
c = a*b;
d = a/b;
e = a^b;
A(1,i) = a;
B(1,i) = b;
C(1,i) = c;
D(1,i) = d;
E(1,i) = e;
end
But ideally I would like to do something like this:
Data = rand(1,1000);
SolCell = {'A'; 'B'; 'C'; 'D'; 'E'};
for i = 1: size(SolCell,1)
SolCell{i,2} = nan(1,1000);
end
parfor i = 1:1000
a = (Data(1,i) * 526^3)/5;
b = (Data(1,i) * 255^2)/6;
c = a*b;
d = a/b;
e = a^b;
SolCell{1,2}(i) = a;
SolCell{2,2}(i) = b;
SolCell{3,2}(i) = c;
SolCell{4,2}(i) = d;
SolCell{5,2}(i) = e;
end
But this results in a 'variable classification' error, as parfor does not like structures or cells.
Thank you very much in advance for your help!
Carlos

采纳的回答

Edric Ellis
Edric Ellis 2021-2-11
parfor can work with struct and cell, but it's often a bit tricky because you need to satisfy the requirement that the slicing indexing is the first level of indexing. In your example, you're attempting to slice in the last index position. In this case, I would be tempted to have the parfor loop emit a single numeric matrix, and then post-process it into the desired form outside parfor. (Simple reindexing such as that will be more efficient outside parfor anyway). So, something like this (untested...):
out = nan(5, 1000);
parfor i = 1:1000
% ... calculate a,b,c,d,e
% Fill a slice of "out"
out(:, i) = [a,b,c,d,e];
end
% Convert "out"
SolCell = {'A'; 'B'; 'C'; 'D'; 'E'};
for i = 1:size(SolCell,1)
% Pick out a row of "out"
SolCell{i,2} = out(i,:);
end
  1 个评论
Carlos Acasuso
Carlos Acasuso 2021-2-11
That seems to work, many thanks for your help! Super useful.
I think I understand better the indexing requirement, and essentially there is no solution for that since "parfor" does not allow it, but we can always post-process outside of the parfor loop in a (still) efficient way.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by