MATLAB: To generate matrix from an array

matrix arrayvector

Let say I have A=[1; 2; 3; 4; 5; 6; 7; 8] as a single column array. and i want to generate the matrix B and matrix C such that
B=[1 2 3; 2 3 4; 3 4 5; 4 5 6; 5 6 7; 6 7 8]. %if repeation of last two elements of previous row is carried out%
C=[1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7; 5 6 7 8; ] %if repeation of last three elements of previous row is done% and so on
Please help me to code this.

Best Answer

Use implicit expansion. (starting r2016b)
B = A(1:end-2) + (0:2); %assuming A is a column vector

C = A(1:end-3) + (0:3); %assuming A is a column vector