creating matrix from a vector

1 回表示 (過去 30 日間)
Tommaso Decataldo
Tommaso Decataldo 2019 年 10 月 12 日
コメント済み: Tommaso Decataldo 2019 年 10 月 12 日
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))

採用された回答

Stephen23
Stephen23 2019 年 10 月 12 日
>> 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 件のコメント
Tommaso Decataldo
Tommaso Decataldo 2019 年 10 月 12 日
Thank you that’s great

サインインしてコメントする。

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 12 日
編集済み: KALYAN ACHARJYA 2019 年 10 月 12 日
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 件のコメント
Tommaso Decataldo
Tommaso Decataldo 2019 年 10 月 12 日
編集済み: Tommaso Decataldo 2019 年 10 月 12 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by