Question

Is there an easy way to make a band matrix in Scilab, something similar to this code for Mathematica:

mat=Normal@SparseArray[{Band[{1,1}]->1,Band[{1,4}]->2,Band[{3,1}]->-3},{10,10}];

? I know there's sparse() function for creating sparse matrices, which I can then transform to normal form via full(). But it seems I can only specify single entries for sparse(), not bands. Is there something I missed in docs? Or do I have to create it "manually" (i.e. write some procedure for that)?

Était-ce utile?

La solution

I think you're looking for the diag function. It creates a matrix that is large enough to contain the values specified. If you combine this with the linspace function you can create the bands.

Working example

clear;

m = 10;

band1 = sparse(diag( ones(m-1,1),1));
band2 = sparse(diag( linspace(1,4,m-2),2) );
band3 = sparse(diag( linspace(3,1,m-3),-3) );

mat = band1 + band2 + band3;

disp( full(mat) );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top