MATLAB Answers

Repeat a vector with increment

14 views (last 30 days)
John
John on 30 Dec 2016
Edited: Azzi Abdelmalek on 30 Dec 2016
Given
vector a = (1:N)'; with N consecutive integers
how to generate
matrix b = [a a+1 a+2 ... a+M]; with M columns, each column incremented by one.
For example
N = 3; M = 3;
a = (1:3)';
b = [(1:3)' (2:4)' (3:5)' (4:6)'];

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 30 Dec 2016
This works:
N = 3;
M = 3;
a = (1:3)';
b = bsxfun(@plus, a, [0:M-1])
b =
1 2 3
2 3 4
3 4 5

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.