Creating a diagonal matrix

3 visualizaciones (últimos 30 días)
Gratitude Kim
Gratitude Kim el 5 de Ag. de 2017
Editada: Andrei Bobrov el 6 de Ag. de 2017
I will like to create such diagonal matrix. A=[1 1 1 0 0 0; 0 1 1 1 0 0; 0 0 1 1 1 0; 0 0 0 1 1 1] Appreciate your kind assitance.

Respuestas (4)

Matt J
Matt J el 5 de Ag. de 2017
A=conv2(eye(4),[1,1,1])

John D'Errico
John D'Errico el 5 de Ag. de 2017
Editada: John D'Errico el 5 de Ag. de 2017
That is not a diagonal matrix, not by the common definition of the term. It has non-zero diagonals, but that is as far as I might go.
Still trivial though.
triu(tril(ones(4,6),2))
ans =
1 1 1 0 0 0
0 1 1 1 0 0
0 0 1 1 1 0
0 0 0 1 1 1
Or...
toeplitz([1 0 0 0]',[1 1 1 0 0 0])
ans =
1 1 1 0 0 0
0 1 1 1 0 0
0 0 1 1 1 0
0 0 0 1 1 1

Andrei Bobrov
Andrei Bobrov el 5 de Ag. de 2017
Editada: Andrei Bobrov el 6 de Ag. de 2017
full(spdiags(ones(4,3),0:2,4,6))
or
t = true(4,6);
out = tril(t,2) & triu(t)
or
a = triu(ones(4,3));
out = [a,rot90(a,2)]

Matt J
Matt J el 5 de Ag. de 2017
Editada: Matt J el 5 de Ag. de 2017
You could also use interpMatrix ( Download ) in a somewhat weird way,
A=full( interpMatrix( [1,1,1] , 1,4,1,'allcontrib') )

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by