creating matrix from a vector

조회 수: 3 (최근 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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by