generate a matrix from some vectors

1 次查看(过去 30 天)
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. ....

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by