creating matrix from a vector
3 次查看(过去 30 天)
显示 更早的评论
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))
0 个评论
采纳的回答
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 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!