arrays - The fastest way to set up sparse matrix in matlab -
i working iterative methods, , large sparse matrices. instance, want set matrix this:
1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 0 1
so diagonals non-zero. in programming, working larger matrix sizes, idea same: few diagonals non-zero, other entries zeros.
i know, how in loop, seems not effective, if matrix size large. work symmetric matrices. appreciate, if provide me code sample matrix along description.
you want spdiags
:
m = 6; %// number of rows n = 10; %// number of columns diags = [-4 -1 0 1 4]; %// diagonals filled = spdiags(ones(min(m,n), numel(diags)), diags, m, n);
this gives:
>> full(a) ans = 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 0 1
Comments
Post a Comment