Help with making parfor loop work
5 views (last 30 days)
Show older comments
Need help making the code below run in parfor loop. The errors are:
"The parfor loop cannot run due to the way symTable is used"
"The parfor loop cannot run due to the way asymTable is used"
freqVec = freqMin:freqStep:freqMax;
fullModes = 1:4;
symTab = ["Frequency" ; "S"+string(fullModes'-1) + " Cp"];
asymTab = ["Frequency" ; "A"+string(fullModes'-1) + " Cp"];
tableTypes = repmat("double",1,fullModes(end)+1);
symTable = table('Size',[length(freqMin:freqStep:freqMax) fullModes(end)+1],'VariableNames', ...
symTab,'VariableTypes',tableTypes);
asymTable = table('Size',[length(freqMin:freqStep:freqMax) fullModes(end)+1],'VariableNames', ...
asymTab,'VariableTypes',tableTypes);
parfor ii = 1:length(freqVec)
currentFreq = freqVec(ii);
symCTR = 1;
asymCTR = 1;
%LOTS OF CODE HERE TO DETERMINE Cp, which is a 2x10 matrix
for modeType = 1:2
if any(CpFin(1,:) ~= 0,2) % store root results into table based on mode.
symTable(ii,1) = {currentFreq};
asymTable(ii,1) = {currentFreq};
for j = 1:size(CpFin,2)
if modeType == 1
if symCTR > width(symTable)-1
continue
elseif CpFin(1,j) == 0
continue
elseif round(CpFin(1,j)) == Cs || round(CpFin(1,j)) == Cl
continue
end
symTable(ii,symCTR+1) = {CpFin(1,j)};
symCTR = symCTR + 1;
else
if asymCTR > width(asymTable)-1
continue
elseif CpFin(1,j) == 0
continue
elseif round(CpFin(1,j)) == Cs || round(CpFin(1,j)) == Cl
continue
end
asymTable(ii,asymCTR+1) = {CpFin(1,j)};
asymCTR = asymCTR + 1;
end
end
end
end
end
0 Comments
Accepted Answer
Walter Roberson
on 19 Mar 2024
First of all, you are permitted only one assignment into a variable indexed by your parfor loop. Having
symTable(ii,1) = {currentFreq};
and
symTable(ii,symCTR+1) = {CpFin(1,j)};
is not allowed.
What you effectively have to do is assign into a temporary variable, and then assign the temporary variable to symTable(ii,:)
1 Comment
Edric Ellis
on 20 Mar 2024
This is correct. You will also need to move the computation width(symTable) to before the parfor loop. The same applies to asymTable.
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!