14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 Data Structures 139<br />

Matrices<br />

Subscripts<br />

Use the subscript operator ([ ]) to pick out elements or submatrices from matrices. The Subscript()<br />

function is usually written as a bracket notation after the matrix to be subscripted, with arguments for rows<br />

and columns.<br />

Single Element<br />

The expression A[i,j] extracts the element in row i, column j, returning a scalar number. The equivalent<br />

functional form is Subscript(A,i,j).<br />

P=[1 2 3, 4 5 6, 7 8 9];<br />

P[2,3]; // returns 6<br />

Subscript(P,2,3); // returns 6<br />

Assign the value that is in the third row and the first column in the matrix A (which is 5) to the variable<br />

test.<br />

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

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

Show(test);<br />

test = 5;<br />

Matrix or List Subscripts<br />

To extract a sub-matrix, use matrix or list subscripts. The result is a matrix of the selected rows and columns.<br />

The following expressions select the 2nd and 3rd rows with the 2nd and 1st columns.<br />

P=[1 2 3, 4 5 6, 7 8 9];<br />

P[[2 3],[1 3]]; // matrix subscripts<br />

P[{2,3},{1,3}]; // list subscripts<br />

Both of these methods provide the following output:<br />

[4 6,<br />

7 9]<br />

Single Subscripts<br />

A single subscript addresses matrices as if all the rows were connected end-to-end in a single row. This makes<br />

the double subscript A[i,j] the same as the single subscript A[(i-1)*ncol(A)+j].<br />

Examples<br />

Q = [2 4 6,8 10 12,14 16 18];<br />

Q[8];<br />

16<br />

The following examples all return the column vector [10, 14, 18]:<br />

Q = [2 4 6, 8 10 12, 14 16 18];<br />

Q[{5, 7, 9}];<br />

Q[[5 7 9]];

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

Saved successfully!

Ooh no, something went wrong!