0
votes

I'm trying to construct a lower triangular matrix of a particular form, and as of right now, I'm using the following for loop:

M = sparse(eye(DIM));
for k = 1 : DIM - 1
    ak = -z*(1-z)^(k-1);
    M = M + sparse(diag(ak * ones(1, DIM - k), -k));
end

Basically, each diagonal from the main diagonal down is constant. I didn't include my definitions for DIM and z since they're not relevant to the construction of the matrix.

Does anyone know a faster way to do this? Maybe a vectorized version?

1
Looks like a Toeplitz matrix, only sparse. Then again if half your matrix is full, I'm not sure it makes sense to keep it sparse. - Andras Deak
Ahhh, Toeplitz matrix! Thank you! That helps. And regarding the sparsity, I see your point. :) - EpicMochi

1 Answers

2
votes

As Andras mentioned in a comment, you can use toeplitz to help you generate this without the need for a loop. You can use the two-input form (supplying the first column and row contents) to create a non-symmetric matrix like so:

M = toeplitz([1 -z.*(1-z).^(0:DIM-2)], [1 zeros(1, DIM-1)]);

This will be a full matrix, so you'll have to convert it to sparse if you need it in that form (although it may not save you anything memory-wise, since your matrix will be half-full).