creating matrix from a vector

1 visualización (últimos 30 días)
Tommaso Decataldo
Tommaso Decataldo el 12 de Oct. de 2019
Comentada: Tommaso Decataldo el 12 de Oct. de 2019
let's suppose I have this vector
v=[0; 0; 4; 6]
and I want to create a matrix such that
X=[0,0,0; 0,0,0; 4,4,0; 6,6,6]
does it exist a way through for cylce to obtain a result like this?
My aim is to create such a matrix in order to discount coupon premiums for different bonds all in a matrix (X), so the repetition of the values are the number of frequency of each cashflow. as in the example:
first two elements of the vector v represent zero coupon, so the repetition will be zero
the third one is 4 and it provides 2 cashflows ->(Maturity=1 year; freq=0.5 -> (1/0.5))
the last one is 6 and provide 3 cashflows ->(Maturity=1.5; freq=0.5 -> (1.5/0.5))

Respuesta aceptada

Stephen23
Stephen23 el 12 de Oct. de 2019
>> v = [0,0,4,6]
v =
0 0 4 6
>> n = ceil(v/2);
>> w = 1:max(n);
>> m = bsxfun(@times,v,bsxfun(@le,w(:),n))
m =
0 0 4 6
0 0 4 6
0 0 0 6
Or for MATLAB versions >=R2016b:
m = v .* (w(:)<=n)

Más respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 12 de Oct. de 2019
Editada: KALYAN ACHARJYA el 12 de Oct. de 2019
let's suppose I have this vector
v=[0; 0; 4; 6]
and I want to create a matrix such that
X=[0,0,0; 0,0,0; 4,4,0; 6,6,6]
% it may be..........^4
One way:
v=[0; 0; 4; 6];
X=reshape(repelem(v,3),[3,4])'
Rest text lines, I didn't go through, generally I avoid to read long texts.
Hope you get the first answer.
  1 comentario
Tommaso Decataldo
Tommaso Decataldo el 12 de Oct. de 2019
Editada: Tommaso Decataldo el 12 de Oct. de 2019
This is not bad but my real problem is that
X=[0,0,0; 0,0,0; 4,4,0; 6,6,6]
% it may be ..........^4 (not)
the matrix should be exactly like I wrote.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by