Creating a matrix having repeating elements

1 vue (au cours des 30 derniers jours)
Rengin
Rengin le 4 Sep 2019
Commenté : Rengin le 4 Sep 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 commentaires
Bruno Luong
Bruno Luong le 4 Sep 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 le 4 Sep 2019
Thanks but I was still be able to get my answer. It works perfectly fine.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 4 Sep 2019
Modifié(e) : Andrei Bobrov le 4 Sep 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 commentaires
madhan ravi
madhan ravi le 4 Sep 2019
You always impress without a doubt , a +1 from me.
Andrei Bobrov
Andrei Bobrov le 4 Sep 2019
Thank you Madhan!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by