Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Paolo
Paolo am 26 Jun. 2018
Bearbeitet: Paolo am 26 Jun. 2018
You mentioned the matrix you want to obtain is a 100x100 matrix. Should the matrix in your example be 5x5, or 8x8?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 26 Jun. 2018
Bearbeitet: John D'Errico am 26 Jun. 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.

Weitere Antworten (2)

Image Analyst
Image Analyst am 26 Jun. 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 am 26 Jun. 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!
  1 Kommentar
Ravikumar Gogar
Ravikumar Gogar am 26 Jun. 2018
I am getting following error Undefined function 'convmtx' for input arguments of type 'double'.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by