creating matrix from a vector

1 vue (au cours des 30 derniers jours)
Tommaso Decataldo
Tommaso Decataldo le 12 Oct 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))

Réponse acceptée

Stephen23
Stephen23 le 12 Oct 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)
  1 commentaire
Tommaso Decataldo
Tommaso Decataldo le 12 Oct 2019
Thank you that’s great

Connectez-vous pour commenter.

Plus de réponses (1)

KALYAN ACHARJYA
KALYAN ACHARJYA le 12 Oct 2019
Modifié(e) : KALYAN ACHARJYA le 12 Oct 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 commentaire
Tommaso Decataldo
Tommaso Decataldo le 12 Oct 2019
Modifié(e) : Tommaso Decataldo le 12 Oct 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by