Select next discrete cell by vector direction

조회 수: 2 (최근 30일)
Rafael Luque
Rafael Luque 2019년 10월 3일
댓글: Rafael Luque 2019년 10월 3일
The situation is: I have my space discretized on cells (dimension doesn't matter) so that I'm at any cell and I know the wind flow 3 dimensional vector in that point.
What I would like to do is move to the most convenient cell next, depending on the wind vector direction.
Clarifications:
  • From each center cell, I can move in the 26 directions (taking into account diagonals) (I have painted some of them in the picture below).
  • I have cells classified by numbers (like in the image)
So, for example, if my wind vector is (400,0,0) I would have to move in the x axis positive direction to the '22' cell. But if my wind vector is (100,100,100) I would have to move in the positive diagonal (x,y,z) to the '26' cell (see image).
It gets more complicated when the vector is not so evident, for example (100,10,0). In this case the most convenient cell is number 22 too, because it is the closest.

채택된 답변

Daniel M
Daniel M 2019년 10월 3일
편집: Daniel M 2019년 10월 3일
You have the distance to each cell as the following:
[x,y,z] = ndgrid(-1:1);
R = [z(:),y(:),x(:)];
R(14,:) = []; % since you do not consider the center cell (0,0,0)
C = 1:26; % this coordinates with the (x,y,z) position in R
Now you need to get a normalized wind vector:
wind = [100,10,0];
nwind = normalize(wind,'norm');
Then finally, find the minimum euclidean distance between nwind and R. There are several ways to do this, here is one:
edist = sqrt(sum((nwind-R).^2,2));
[~,ind] = min(edist);
nextCell = C(ind); % the next cell to move to
And the output is:
nextCell =
22
  댓글 수: 3
Daniel M
Daniel M 2019년 10월 3일
편집: Daniel M 2019년 10월 3일
Replace nwind with repmat(nwind, length( R ),1), or try replacing nwind-R with bsxfun(@minus,nwind,R)
Rafael Luque
Rafael Luque 2019년 10월 3일
Thank you very much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Inertias and Loads에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by