MATLAB: Generate a matrix from some vectors

MATLABmatrix from vectors

Hi, I have an array A0 (with 1 by n elements), representing a sample with n variables. I also have two arrays LB and UB (each with 1 by n elements) representing the lower and upper bounds for our variables.
How can I quickly generate a matrix B (2n+1 by n) from A0, LB and UB in such a way that
1. the first row is A0
2. the second row is A0 but A0(1,1) is replaced by LB(1,1)
3. the third row is A0 but A0(1,1) is replaced by UB(1,1)
4. The fourth row is A0 but A0(1,2) is replaced by LB(1,2)
5. The fifth row is A0 but A0(1,2) is replaced by UB(1,2)
6. The sixth row is A0 but A0(1,3) is replaced by LB(1,3)
7. The seventh row is A0 but A0(1,3) is replaced by UB(1,3)
8. The eights row is A0 but A0(1,4) is replaced by LB(1,4)
9. ….

Best Answer

% sample values
n = 10;
a = rand(1, n);
LB = 1:n;
UB = LB + 1;
A = repmat(a, [2*n , 1]);
idx = 1:2*n+2:2*n*n;
A(idx) = LB;
A(idx + 1) = UB;