14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

152 Data Structures Chapter 7<br />

Matrices<br />

0 1 0,<br />

0 0 1]<br />

Construct a Matrix with Specific Values<br />

The J() function constructs a matrix with the number of rows and columns that you specify as the first two<br />

arguments, whose elements are all the third argument, for example:<br />

J(3,4,5);<br />

[5 5 5 5,<br />

5 5 5 5,<br />

5 5 5 5]<br />

J(3,4,random normal());<br />

[0.407709113182904 1.67359154091978 1.00412665221308 0.240885679837327,<br />

-0.557848036549455 -0.620833861982722 0.877166783247633 1.50413740148892,<br />

-2.09920574748608 -0.154797501010655 0.0463943433032137 0.064041826393316]<br />

Create a Diagonal Matrix<br />

The Diag() function creates a diagonal matrix from a square matrix (having an equal number of rows and<br />

columns) or a vector. A diagonal matrix is a square matrix whose nondiagonal elements are zero.<br />

D=[ 1 -1 1];<br />

Diag(D);<br />

[1 0 0,<br />

0 -1 0,<br />

0 0 1]<br />

Diag([1,2,3,4]);<br />

[1 0 0 0,<br />

0 2 0 0,<br />

0 0 3 0,<br />

0 0 0 4]<br />

A=[1 2,3 4];<br />

f=[5];<br />

D=Diag(A,f);<br />

[1 2 0<br />

3 4 0<br />

0 0 5]<br />

In the third example, at first glance, not all of the nondiagonal elements are zero. Using matrix notation, the<br />

matrix can be expressed as follows:<br />

[A 0,<br />

0` f]<br />

Where A and f are the matrices from the example, and 0 is a column vector of zeros.<br />

Create a Column Vector from Diagonal Elements<br />

The VecDiag() function creates a column vector from the diagonal elements of a matrix.<br />

v=vecdiag(<br />

[1 0 0 1,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!