Hot to create a matrix with elements which moves to right as row increases
3 次查看(过去 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!