Creating a matrix using a loop

조회 수: 1(최근 30일)
Mohammad
Mohammad 2013년 8월 12일
Hi,
I am struggling with probably a very simple problem. I want to build a matrix. My X coordinate runs from -2 to 2 with increment 1 and same with Y. I want to create a matrix that will be following: x: -2 -1 0 1 2 Y: -2 -2 -2 -2 -2, then goes on X: -2 -1 0 1 2, Y= -1 -1 -1 -1 -1 and continues until X= -2 -1 0 1 2 , Y=2 2 2 2 2.
This means for same X positions Y varies from -2 to 2. I dont know how to do that. Could anybody please help me on this.
Thanks,
Rafiq

채택된 답변

Matt Kindig
Matt Kindig 2013년 8월 12일
[xx,yy]=ndgrid( -2:1:2, -2:1:2);
xy = [xx(:), yy(:)];
x = xy(:,1);
y = xy(:,2);
  댓글 수: 1
Mohammad
Mohammad 2013년 8월 13일
Thanks. It helped.

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

추가 답변(1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 12일
편집: Azzi Abdelmalek 2013년 8월 12일
y=repmat(x,numel(x),1)
y(:,1) % the first vector y
y(:,2) % the second
% and so on

범주

Find more on Creating and Concatenating Matrices 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!

Translated by