Creating a matrix having repeating elements

1 visualización (últimos 30 días)
Rengin
Rengin el 4 de Sept. de 2019
Comentada: Rengin el 4 de Sept. de 2019
% Dear users,
% Assume that I have a A matrix having the size of 5x5 and in the end;
% I want to get A=[1 -1 0 0 0; 1 -1 0 0 0; 0 1 -1 0 0; 0 1 -1 0 0; 0 0 1 -1 0]
% What is the easiest way to do it?
% Thanks in advance
  6 comentarios
Bruno Luong
Bruno Luong el 4 de Sept. de 2019
OK here is different and still simple:
A=[1 -1 0 0 0; 1 -1 0 0 0; 0 1 -1 0 0; 0 1 -1 0 0; 0 0 1 -1 0]+0
The point is your question is not well formulated
Rengin
Rengin el 4 de Sept. de 2019
Thanks but I was still be able to get my answer. It works perfectly fine.

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 4 de Sept. de 2019
Editada: Andrei Bobrov el 4 de Sept. de 2019
A = kron(eye(5),[1;1])+kron(diag(-ones(4,1),1),[1;1]);
out = A(1:5,:);
or
n = 1:5;
A = repmat([1,-1,0,0,0],5,1);
out = A(mod(repmat(n,5,1) - ceil(n(:)/2),5)*5 + n(:));
  2 comentarios
madhan ravi
madhan ravi el 4 de Sept. de 2019
You always impress without a doubt , a +1 from me.
Andrei Bobrov
Andrei Bobrov el 4 de Sept. de 2019
Thank you Madhan!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical 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