How can one create this matrix?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
更多回答(1 个)
Alan Stevens
2020-7-24
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!