generate a matrix from some vectors

1 visualización (últimos 30 días)
H R
H R el 30 de Mzo. de 2017
Respondida: Thorsten el 31 de Mzo. de 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. ....

Respuesta aceptada

Thorsten
Thorsten el 31 de Mzo. de 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;

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by