Combination of X and Y vectors to get all possible positions on a Cartesian plane

조회 수: 14 (최근 30일)
Dear Community,
I have a challenging task that I am not able to solve so far: I have a vector X of 'n' elements (in my case those are X coordinates) and I have another vector Y of 'm' elements (in my case Y coordinate). I just want to creat a new vector Z with n*m rows and 2 colums that lists within all possible combinations of points on the XY Cartesian plane.
Example:
X=1,2,3,4 (n=4)
Y=5,6 (m=2)
then Z= 1,5 1,6 2,5 2,6 3,5 3,6 and so on....

채택된 답변

James Tursa
James Tursa 2019년 5월 20일
[XX,YY] = ndgrid(X,Y);
Z = [XX(:),YY(:)];
  댓글 수: 1
Maximilian Arpaio
Maximilian Arpaio 2019년 5월 20일
Thanks a lot James.
Actually, surfing better among the already available answers (my fault sorry) I've also found:
[A,B] = meshgrid(X,Y);
c=cat(2,A',B');
Z=reshape(c,[],2);
coming from this post: LINK

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2019년 5월 21일
For two vectors, x and y, this might be faster than ndgrid (not tested)
xy = [repelem(x(:), numel(y), 1) repmat(y(:), numel(x), 1)]

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by