Hot to create a matrix with elements which moves to right as row increases
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
John D'Errico
2018년 6월 26일
편집: John D'Errico
2018년 6월 26일
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.
댓글 수: 0
추가 답변 (2개)
Image Analyst
2018년 6월 26일
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)
댓글 수: 0
Shweta Singh
2018년 6월 26일
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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!