Creating a matrix having repeating elements

% 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 Commenti

Is this matrix what you want? I don't understand the structure
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
Rengin
Rengin il 4 Set 2019
Modificato: Rengin il 4 Set 2019
Yes, that's right! the element of (1,-1) should shift one column to the right in every odd row. If the row has an even number, it should repeat itself.
The easiest way is just type you matrix.
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]
Rengin
Rengin il 4 Set 2019
Modificato: Rengin il 4 Set 2019
Since I can do it by myself, I am asking obviously something different.
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
Thanks but I was still be able to get my answer. It works perfectly fine.

Accedi per commentare.

 Risposta accettata

Andrei Bobrov
Andrei Bobrov il 4 Set 2019
Modificato: Andrei Bobrov il 4 Set 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 Commenti

You always impress without a doubt , a +1 from me.
Thank you Madhan!

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by