How can I create a new matrix by using existing values in a column?

I've a column inluding the values of
>> B=[0.100;0.250;0.200;0.120;0.080;0.060;0.055;0.040;0.020]
B =
0.1000
0.2500
0.2000
0.1200
0.0800
0.0600
0.0550
0.0400
0.0200
But I want to make it as
0.100 0 0 0 0 0 0 0 0
0.250 0.100 0 0 0 0 0 0 0
0.200 0.250 0.100 0 0 0 0 0 0
0.120 0.200 0.250 0.1 0 0 0 0 0
0.080 0.120 0.200 0.250 0.1 0 0 0 0
0.060 0.080 0.120 0.200 0.25 0.1 0 0 0
0.055 0.060 0.080 0.120 0.2 0.25 0.1 0 0
0.040 0.055 0.060 0.080 0.12 0.2 0.25 0.1 0
0.020 0.040 0.055 0.060 0.08 0.12 0.2 0.25 0.1
How can I do it?

 Accepted Answer

Stephen23
Stephen23 on 13 Dec 2018
Edited: Stephen23 on 13 Dec 2018
Simpler and much more efficient to use toeplitz:
>> B = [0.100;0.250;0.200;0.120;0.080;0.060;0.055;0.040;0.020];
>> C = zeros(size(B));
>> C(1) = B(1);
>> M = toeplitz(B,C)
M =
0.100 0 0 0 0 0 0 0 0
0.250 0.100 0 0 0 0 0 0 0
0.200 0.250 0.100 0 0 0 0 0 0
0.120 0.200 0.250 0.100 0 0 0 0 0
0.080 0.120 0.200 0.250 0.100 0 0 0 0
0.060 0.080 0.120 0.200 0.250 0.100 0 0 0
0.055 0.060 0.080 0.120 0.200 0.250 0.100 0 0
0.040 0.055 0.060 0.080 0.120 0.200 0.250 0.100 0
0.020 0.040 0.055 0.060 0.080 0.120 0.200 0.250 0.100

More Answers (2)

B=[0.100;0.250;0.200;0.120;0.080;0.060;0.055;0.040;0.020];
n = length(B);
% prepare indices
idx = 0:(length(B)-1);
% splicing and padding
func = @(i) padarray(B(1:n-i), i, 'pre');
% execute padding and splicing element-wise
c = arrayfun(func, idx, 'UniformOutput', false);
result = cell2mat(c)
result =
0.1000 0 0 0 0 0 0 0 0
0.2500 0.1000 0 0 0 0 0 0 0
0.2000 0.2500 0.1000 0 0 0 0 0 0
0.1200 0.2000 0.2500 0.1000 0 0 0 0 0
0.0800 0.1200 0.2000 0.2500 0.1000 0 0 0 0
0.0600 0.0800 0.1200 0.2000 0.2500 0.1000 0 0 0
0.0550 0.0600 0.0800 0.1200 0.2000 0.2500 0.1000 0 0
0.0400 0.0550 0.0600 0.0800 0.1200 0.2000 0.2500 0.1000 0
0.0200 0.0400 0.0550 0.0600 0.0800 0.1200 0.2000 0.2500 0.1000

1 Comment

@TADA thanks for your answer. That's exactly what I 'm looking for.
@Mark Sherstan, thanks for your answer. It worked but TADA's answer is more suitable for me.

Sign in to comment.

Try this:
B = [0.100;0.250;0.200;0.120;0.080;0.060;0.055;0.040;0.020]
A = zeros(1,length(B));
for ii = 1:length(B)
A = circshift(A,1);
A(1) = B(ii)
end

Categories

Asked:

on 12 Dec 2018

Edited:

on 13 Dec 2018

Community Treasure Hunt

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

Start Hunting!