Slicing variable in parfor loop (restricted indexing)

13 vues (au cours des 30 derniers jours)
Rafael Schwarzenegger
Rafael Schwarzenegger le 3 Fév 2020
Hello,
would somebody please know, why this code isn't sliced? Which is the problematic part. I was trying to figure it out, but unfortunately not successful.
parfor i=1:size(X,1)
for j=3:numel(r)
check = 0;
if sum(X(i,end-5:end-4)<=0) > 0 || any(isnan(X(i,end-5:end-4)))
check = 1;
end
if check == 0
X(i,j) = fn( X(i,end-1:end),...
r(j),[X(i,end-5) X(i,10)],[X(i,end-4) X(i,11)]);
end
end
end
Thanks very much in advance for any help and effort!

Réponse acceptée

Edric Ellis
Edric Ellis le 4 Fév 2020
Steve has already pointed out that your X indexing expressions make it ineligible for slicing as things stand. However, fortunately each parfor loop iteration is accessing only a single row of X, so your code can be adapted to the restrictions of parfor sliced variable indexing quite simply by extracting a row of X at the start of the loop iteration, working on that, and then putting it back again at the end. Like this:
parfor i=1:size(X,1)
% Extract a single row of X
xRow = X(i, :);
% Operate on xRow, not X
for j=3:numel(r)
check = 0;
if sum(xRow(end-5:end-4)<=0) > 0 || any(isnan(xRow(end-5:end-4)))
check = 1;
end
if check == 0
xRow(j) = fn( xRow(end-1:end),...
r(j),[xRow(end-5) xRow(10)],[xRow(end-4) xRow(11)]);
end
end
% Put the updated xRow back into X.
X(i, :) = xRow;
end
  1 commentaire
Rafael Schwarzenegger
Rafael Schwarzenegger le 4 Fév 2020
That is working well. Thank you very much!!! :)

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 3 Fév 2020
What requirements must a variable satisfy to be a sliced variable? The documentation states four conditions:
  • Type of First-Level Indexing — The first level of indexing is either parentheses, (), or braces, {}.
  • Fixed Index Listing — Within the first-level parentheses or braces, the list of indices is the same for all occurrences of a given variable.
  • Form of Indexing — Within the list of indices for the variable, exactly one index involves the loop variable.
  • Shape of Array — The array maintains a constant shape. In assigning to a sliced variable, the right side of the assignment cannot be [] or '', because these operators attempt to delete elements.
I'm assuming you want X in your example to be the sliced variable. All the places you index into X do use parentheses, so you satisfy "Type of First-Level Indexing". Do you satisfy "Fixed Index Listing"? Here are the set of indices you use to index into X:
  • (i,end-5:end-4)
  • (i,end-5:end-4)
  • (i,j)
  • (i,end-1:end)
  • (i,end-5)
  • (i,10)
  • (i,end-4)
  • (i,11)
So no, your variable does not satisfy the "Fixed Index Listing" condition. It does satisfy "Form of Indexing" and "Shape of Array". You might be able to do something along the lines of the second example in the "Fixed Index Listing" section on that documentation page, but I haven't tried to fully understand the code you've written nor do I know what function you're using as fn so I can't be certain.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by