I want to create a matrix like
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
..
That is, I start from
grids=1:1:5
and I want to repeat this "grids" in the second column. Please advise.

 채택된 답변

madhan ravi
madhan ravi 2020년 7월 24일

0 개 추천

[X, Y] = meshgrid(1:5);
[X(:), Y(:)]

추가 답변 (1개)

Alan Stevens
Alan Stevens 2020년 7월 24일

0 개 추천

This will do it:
n = 5; reps = 4;
c1 = ones(n,1)*(1:reps); c1 = c1(:);
c2 = (ones(reps,1)*(1:n))'; c2=c2(:);
M = [c1 c2];

댓글 수: 3

madhan ravi
madhan ravi 2020년 7월 24일
편집: madhan ravi 2020년 7월 24일
But why so complicated? And the first column doesn’t have 5 in it? In fact a simple call to ndgrid() or meshgrid() would do the job?
Alan Stevens
Alan Stevens 2020년 7월 24일
편집: Alan Stevens 2020년 7월 24일
You are right that there are probably easier ways to do it (use of meshgrid would indeed be better, but I just didn't think of it!)
The first column doesn't have a 5 in it because it wasn't clear to me that the op wanted the same number of repeats as grids.
alpedhuez
alpedhuez 2020년 7월 24일
Thank you any way.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2020년 7월 24일

댓글:

2020년 7월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by