How to slice these variables?

조회 수: 4 (최근 30일)
Joerg Pfannmoeller
Joerg Pfannmoeller 2021년 11월 9일
댓글: Joerg Pfannmoeller 2022년 3월 14일
Hi,
can somebody help me to slice these variables in a parfor loop?
indices(:,1) = max(min(ceil(vector(:,1)-Xcoor),nx),1);
indices(:,2) = max(min(ceil(vector(:,2)-Ycoor),ny),1);
indices(:,3) = max(min(ceil(vector(:,3)-Ycoor(idx_parfor)),nz),1);
I currently use the construction:
indices_1 = max(min(ceil(vector(:,1)-Xcoor),nx),1);
indices_2 = max(min(ceil(vector(:,2)-Ycoor),ny),1);
indices_3 = max(min(ceil(vector(:,3)-Ycoor(idx_parfor)),nz),1);
Is there any way to use a single variable indices again? In order to allow for indexing of the variable indices? The parfor variable is of course idx_parfor. An indexed solution would be unavoidable if there are more than 3 indices.
Best Joerg
  댓글 수: 2
Rik
Rik 2021년 11월 9일
If it is merely the detection that is preventing execution, you could consider putting this in a separate function.
Also, shouldn't you be using Zcoor in your third call?
Joerg Pfannmoeller
Joerg Pfannmoeller 2022년 3월 14일
Thank you. There is a type-o in indices_3, as you found.

댓글을 달려면 로그인하십시오.

채택된 답변

Edric Ellis
Edric Ellis 2021년 11월 9일
I think what you're trying to do is something like this, which doesn't work:
N = 4;
out = zeros(N, 3);
try
parfor idx = 1:N
out(idx,1) = idx;
out(idx,2) = -idx;
out(idx,3) = 2*idx;
end
catch E
disp(E.message)
end
Error: Unable to classify the variable 'out' in the body of the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops".
The way to fix this is to assign a whole column of out all in one indexing expression:
N = 4;
out = zeros(N, 3);
parfor idx = 1:N
o1 = idx;
o2 = -idx;
o3 = 2*idx;
% Single sliced indexing assignment into `out`
out(idx,:) = [o1, o2, o3];
end
disp(out)
1 -1 2 2 -2 4 3 -3 6 4 -4 8
In some situations, you can use for loops inside parfor.
N = 4;
M = 3;
out = zeros(N, M);
parfor idx = 1:N
% inner for-loop over the columns of `out`
for jdx = 1:M
out(idx,jdx) = 1000 * idx + jdx;
end
end
disp(out)
1001 1002 1003 2001 2002 2003 3001 3002 3003 4001 4002 4003

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by