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?

 Réponse acceptée

Stephen23
Stephen23 le 13 Déc 2018
Modifié(e) : Stephen23 le 13 Déc 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

Plus de réponses (2)

TADA
TADA le 12 Déc 2018
Modifié(e) : TADA le 12 Déc 2018
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 commentaire

Onur Güven
Onur Güven le 13 Déc 2018
Modifié(e) : Onur Güven le 13 Déc 2018
@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.

Connectez-vous pour commenter.

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

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by