Alternative ways to slice/manage data using PARFOR
7 views (last 30 days)
Show older comments
Carlos Acasuso
on 10 Feb 2021
Commented: Carlos Acasuso
on 11 Feb 2021
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
0 Comments
Accepted Answer
Edric Ellis
on 11 Feb 2021
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
More Answers (0)
See Also
Categories
Find more on Parallel for-Loops (parfor) 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!