How do I create a sparse matrix of the following form?

Hello.
Im trying to create a sparse matrix for some vector k, for example [1;2;3], of the following form:
k = [1;2;3];
1 0 0
2 1 0
M = 3 2 1
0 3 2
0 0 3
I can not undarstand how to use 'sparse' function to do it. Many thanks for any suggestions!

1 Comment

Yeah, I did. I dont understand how to do it without a loop. I've tried s=full(sparse(1:3,1,k,5,3)). Cant add k to 2-nd and 3-rd column.

Sign in to comment.

 Accepted Answer

toeplitz(sparse([1,2,3,0,0]) ,sparse([1 0 0]))

3 Comments

Thanks alot Matt J. This function is just what I was looking for.
PS: Nice joke btw ;)
Yeah, sparse joke. About sparse function.
Weren't you joking? Because there is no need to use it in your code.

Sign in to comment.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!