generate a matrix from some vectors

1 view (last 30 days)
H R
H R on 30 Mar 2017
Answered: Thorsten on 31 Mar 2017
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. ....

Accepted Answer

Thorsten
Thorsten on 31 Mar 2017
% 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;

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!