Function diagonal,A,d ;+ ;r=diagonal(A,d) returns diagonal "d" of array A. If d GE 0 ;then the diagonal that starts at position index d along ;the top row of the array is returned. If "d" is negative ;then the diagonal that starts on the side of the array ;at row |d| is returned. ; ;NOTE: Kept to support old code. It is better to use the ;DIAG_MATRIX function in IDL. ; ;Harvey Rhody ;January, 2003 ;Used as a midterm exam question for ;SIMG-726. ;- ; ;If d is not given then get the main diagonal If n_params() EQ 1 THEN d=0 ;Default diagonal ; ;Check to see that the array is only 2D, not 3D or more sa=Size(A,/dim) If n_elements(sa) GT 2 THEN MESSAGE,'Array must be 1D or 2D' ; ;Get the number of rows and columns. The m=1 row situation ;must be handled as a special case since the size vector ;would only have one element in that case. n=sa[0] ;Number of columns m=1 ;Handle the one-row case If n_elements(sa) EQ 2 THEN m=sa[1] ; ;Get the diagonal information IF d GE 0 THEN BEGIN ;Find the diagonal for nonnegative d IF d GE n Then Message,'Diagonal does not exist' ;Find the length of the selected diagonal len=min([m,n-d]) ;Build an index that walks down those positions k=indgen(len)*(n+1)+d ;Select and return the values. Return,A[k] ENDIF ELSE BEGIN ;Find the diagonal for negative d If ABS(d) GE m Then Message,'Diagonal does not exist' ;Find the length of the diagonal len=min([n,m+d]) ;Build an index that walks down the positions. k=Indgen(len)*(N+1)-d*N ;Select and return the values. Return,A[k] ENDELSE END