Hello all,
I want to create this matrix
A=[0 0 1 1 1;0 1 1 1 0;1 1 1 0 0]
How can I get ?
Thank you.

 Respuesta aceptada

Star Strider
Star Strider el 13 de Abr. de 2014
Editada: Star Strider el 13 de Abr. de 2014

0 votos

One way:
v = [0 0 1 1 1 ];
A = [v; circshift(v,[0 -1]); circshift(v,[0 -2])]

2 comentarios

Thewall
Thewall el 13 de Abr. de 2014
Thanks for your answer.
However, I have many columns and rows. I think I should shift 100 times of v vector. How can I do this?
Star Strider
Star Strider el 13 de Abr. de 2014
Editada: Star Strider el 13 de Abr. de 2014
My pleasure!
Use a for loop. If all you want to do is to shift the columns as you described for each row, change my example code to the more generalised version:
v = [0 0 1 1 1 ];
nrows = 3;
for k1 = 1:nrows
A(k1,:) = circshift(v,[0 1-k1]);
end
Change v to the vector you want, and change the number of rows you want to create (denoted here as the value of the variable nrows) to create your matrix.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Abr. de 2014

Editada:

el 13 de Abr. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by