BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
arbongard
Calcite | Level 5

How would I write a subrountine that takes 2 parameters (a constant and a square matrix). Then adds that constant to the every element of the diagonal of the matrix, and passes the results back in a new parameter?

Then out puts the new matrix after the subtracted constant

I have the general idea but getting stuck on the diagonal part.

1 ACCEPTED SOLUTION

Accepted Solutions
Hutch_sas
SAS Employee

A quick way to add a constant to the diagonal elements of a matrix:

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

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

View solution in original post

1 REPLY 1
Hutch_sas
SAS Employee

A quick way to add a constant to the diagonal elements of a matrix:

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

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 1235 views
  • 0 likes
  • 2 in conversation