How can I input a vector into a sparse matrix instead of a single number?

1 view (last 30 days)
Hello,
I'm trying to form a matrix of the form:
[1 1 0 0 0 0 0 0; 0 0 1 1 0 0 0 0; 0 0 0 0 1 1 0 0; 0 0 0 0 0 0 1 1]
using the sparse function. I'm able to get the first diagonal of ones (i.e. [1 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 0; 0 0 0 0 1 0 0 0; 0 0 0 0 0 0 1 0]
using x=1:4 y=1:2:8 mat=sparse(x,y,1,4,8).
What I figured I could do would be to define a=[1,1] and do mat=sparse(x,y,a,4,8)
but matlab doesn't like that I'm trying to give a vector of a difference length from x and y to the spot (and I don't want it to be the same length as x and y, because then it just makes the matrix I can already make by just putting a 1 instead of a).
Does anyone know how to work around this?
Thanks!
-Libby

Accepted Answer

Matt J
Matt J on 26 Jul 2013
mat = kron(speye(4),[1,1])
  3 Comments
Libby
Libby on 26 Jul 2013
Also, any chance you could give an explanation of what is happening here? I've never used kron or speye and the kron explanation on the matlab website isn't doing it for me.

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!