Creating Diagonal Matrix from a Vector
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Piyush Gupta
am 16 Nov. 2020
Beantwortet: KSSV
am 16 Nov. 2020
I have a vector g = [g0 g1 g2 g3 ... gx]
I want to create a matrix of the form:

Here x = (m-n)
Any thoughts on how I can do this?
0 Kommentare
Akzeptierte Antwort
Stephen23
am 16 Nov. 2020
The efficient MATLAB approach:
g = [1,2,3,4,5];
z = zeros(1,numel(g)-1);
m = toeplitz([g(1),z],[g,z])
0 Kommentare
Weitere Antworten (3)
Ameer Hamza
am 16 Nov. 2020
Bearbeitet: Ameer Hamza
am 16 Nov. 2020
This is one way
g = [1 2 3 4 5];
n = numel(g);
M_ = [eye(n) zeros(n,n-1)];
M = zeros(n, 2*n-1);
for i = 1:n
M = M + circshift(M_*g(i), i-1, 2);
end
Result
>> M
M =
1 2 3 4 5 0 0 0 0
0 1 2 3 4 5 0 0 0
0 0 1 2 3 4 5 0 0
0 0 0 1 2 3 4 5 0
0 0 0 0 1 2 3 4 5
0 Kommentare
Bruno Luong
am 16 Nov. 2020
Bearbeitet: Bruno Luong
am 16 Nov. 2020
>> g=[1 2 3]
g =
1 2 3
>> p=length(g);
>> s=10;
>> A=full(spdiags(repmat(g,s,1),0:p-1,s,s+p-1))
A =
1 2 3 0 0 0 0 0 0 0 0 0
0 1 2 3 0 0 0 0 0 0 0 0
0 0 1 2 3 0 0 0 0 0 0 0
0 0 0 1 2 3 0 0 0 0 0 0
0 0 0 0 1 2 3 0 0 0 0 0
0 0 0 0 0 1 2 3 0 0 0 0
0 0 0 0 0 0 1 2 3 0 0 0
0 0 0 0 0 0 0 1 2 3 0 0
0 0 0 0 0 0 0 0 1 2 3 0
0 0 0 0 0 0 0 0 0 1 2 3
% This work as well
>> A = toeplitz([g(1) zeros(1,s-1)],[g zeros(1,s-1)]);
0 Kommentare
KSSV
am 16 Nov. 2020
g = rand(1,4) ;
m = length(g) ;
P =zeros(m) ;
d=size(diag(P,i),1);%this is the size of the vector with elements of the kth diagonal
for i = 1:m
e=g(i)*ones(m+1-i,1);
P = P+diag(e,i-1);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!