Statistical programming, matrix languages, and more

subroutine

Reply
New Contributor
Posts: 3

subroutine

what is an example of a subroutine module with 2 parameters, a constant and a square matrix.

that adds the constant value to every element of the diagonal of the matrix, and passes  the result

back in a new parameter? I'm having a hard time understanding this stuff so lots of comments would

be unbelievably helpful.

SAS Employee
Posts: 94

Re: subroutine

proc iml;

start AddToDiag( result, A, B);

    /* add B to diagonal of a square matrix A */

    result = A + diag(  j(nrow(A),  1,   B)  );

finish;

/* test it out */

m = {1 1 1,

        2 2 2,

        3 3 3 };

lambda = 3;

run AddToDiag( MPlusLambda, m, lambda);

print M lambda MPlusLambda;

quit;

Ask a Question
Discussion stats
  • 1 reply
  • 280 views
  • 0 likes
  • 2 in conversation