Azzera filtri
Azzera filtri

Hot to create a matrix with elements which moves to right as row increases

1 visualizzazione (ultimi 30 giorni)
Hi, I want to create a Matrix A such that
A = [-1 1 -1 0 0 0 0
0 -1 1 -1 0 0 0
0 0 -1 1 -1 0 0
0 0 0 -1 1 -1 0
0 0 0 0 -1 1 -1]
Above matrix is a sample with just 5 rows but i want to create a matrix of size 100x100.
I have used following method but failed
a=[-1 2 1];
x=100;
A=convmtx(a,x)
Please help.
  2 Commenti
Paolo
Paolo il 26 Giu 2018
Modificato: Paolo il 26 Giu 2018
You mentioned the matrix you want to obtain is a 100x100 matrix. Should the matrix in your example be 5x5, or 8x8?

Accedi per commentare.

Risposta accettata

John D'Errico
John D'Errico il 26 Giu 2018
Modificato: John D'Errico il 26 Giu 2018
There are MANY ways to solve this. I'm not at all sure what the final dimension of the matrix is expected to be. Is it 100x100? While you say that, the matrix you show is not square.
So, IF you want to create a square matrix with that pattern, then this will do the trick:
n = 10;
toeplitz([-1;zeros(n-1,1)]',[-1 1 -1,zeros(1,n-3)])
ans =
-1 1 -1 0 0 0 0 0 0 0
0 -1 1 -1 0 0 0 0 0 0
0 0 -1 1 -1 0 0 0 0 0
0 0 0 -1 1 -1 0 0 0 0
0 0 0 0 -1 1 -1 0 0 0
0 0 0 0 0 -1 1 -1 0 0
0 0 0 0 0 0 -1 1 -1 0
0 0 0 0 0 0 0 -1 1 -1
0 0 0 0 0 0 0 0 -1 1
0 0 0 0 0 0 0 0 0 -1
I've shown what it creates for n=10. Make n=100, and you get a 100x100 matrix.
Do you really want to create a non-square matrix? Still easy enough. You could use toeplitz again. Here, I create the matrix you showed in your example.
n = 7;
toeplitz([-1;zeros(n-3,1)]',[-1 1 -1,zeros(1,n-3)])
ans =
-1 1 -1 0 0 0 0
0 -1 1 -1 0 0 0
0 0 -1 1 -1 0 0
0 0 0 -1 1 -1 0
0 0 0 0 -1 1 -1
Pick n=100, to get a 98x100 array, IF that is really what you wanted.
Lots of other ways.
n = 5;
A = [-eye(n),zeros(n,2)] + [zeros(n,1),eye(n),zeros(n,1)] + [zeros(n,2),-eye(n)]
A =
-1 1 -1 0 0 0 0
0 -1 1 -1 0 0 0
0 0 -1 1 -1 0 0
0 0 0 -1 1 -1 0
0 0 0 0 -1 1 -1
Here is another, using gallery to create a circulant matrix.
n = 10;
A = gallery('circul',[-1 1 -1,zeros(1,n-3)]);
A((n-1):n,1:2) = 0
A =
-1 1 -1 0 0 0 0 0 0 0
0 -1 1 -1 0 0 0 0 0 0
0 0 -1 1 -1 0 0 0 0 0
0 0 0 -1 1 -1 0 0 0 0
0 0 0 0 -1 1 -1 0 0 0
0 0 0 0 0 -1 1 -1 0 0
0 0 0 0 0 0 -1 1 -1 0
0 0 0 0 0 0 0 -1 1 -1
0 0 0 0 0 0 0 0 -1 1
0 0 0 0 0 0 0 0 0 -1
Or, you could have used spdiags. Or sparse. Lets see, maybe I could get tricky? How about conv2?
n = 10;
A = conv2(diag(ones(1,n),1),[-1 1 -1],'same');
A(end) = -1
A =
-1 1 -1 0 0 0 0 0 0 0 0
0 -1 1 -1 0 0 0 0 0 0 0
0 0 -1 1 -1 0 0 0 0 0 0
0 0 0 -1 1 -1 0 0 0 0 0
0 0 0 0 -1 1 -1 0 0 0 0
0 0 0 0 0 -1 1 -1 0 0 0
0 0 0 0 0 0 -1 1 -1 0 0
0 0 0 0 0 0 0 -1 1 -1 0
0 0 0 0 0 0 0 0 -1 1 -1
0 0 0 0 0 0 0 0 0 -1 1
0 0 0 0 0 0 0 0 0 0 -1
So many ways to do this. I can think of at least a couple of other ways as I write this line.

Più risposte (2)

Image Analyst
Image Analyst il 26 Giu 2018
Try changing the "a" from [1,2,1] to [-1,1,-1] and then cropping to 100x100:
a = [-1, 1, -1];
x = 100;
A = convmtx(a, x);
A = A(:, 1:100)

Shweta Singh
Shweta Singh il 26 Giu 2018
Hi Ravikumar,
The code you provided seems to be working fine. It creates a matrix of 100x102. As per your requirement, with three elements in vector 'a', the resultant matrix would be of size with number of columns two more than the number of rows, and not the square matrix.
Can you provide more details if this is not what you want?
Thanks!

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by