split a diagonal matrix
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Elysi Cochin
am 18 Mär. 2019
Kommentiert: Jos (10584)
am 18 Mär. 2019
v = [2 1 4];
D = diag(v)
Having a diagonal matrix, i wanted to split the diagonal matrix as below
M1 = [
2 0 0
0 0 0
0 0 0]
M2 = [
0 0 0
0 1 0
0 0 0]
M3 = [
0 0 0
0 0 0
0 0 4]
working for any dimension diagonal matrix
1 Kommentar
Jos (10584)
am 18 Mär. 2019
What do you mean by "working for any dimension diagonal matrix" ?
Any number of elements in V? In that case, see my answer below.
Akzeptierte Antwort
KSSV
am 18 Mär. 2019
v = [2 1 4];
nx = length(v) ;
M = zeros(nx,nx,nx) ;
for i = 1:nx
M(i,i,i) = v(i) ;
end
0 Kommentare
Weitere Antworten (1)
Jos (10584)
am 18 Mär. 2019
Having a vector V with any number of elements, this does the job:
V = [2 1 4]
n = numel(V) ;
M = zeros(n,n,n) ;
M(linspace(1, n^3, n)) = V
If you have a diagonal matrix D, use V = diag(D) first.
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!