FUNCTION WRESHAPE, A, m, n ;+ ;NAME: ; WRESHAPE ; ;PURPOSE: ; To reshape all elements in a matrix to the desired ; shape, like Matlab's "reshape". ; ;CATEGORY: ; Simple math. ; ;CALLING SEQUENCE: ; Result = WRESHAPE(A, nrows,ncols) ; ;INPUTS: ; A: A matrix ; m: number of rows ; n: number of columns ; ;OUTPUTS: ; The matrix turned into a column with the elements in the right shape. ; ;SIDE EFFECTS: ; If A does not have m*n elements, an error occurs. ; ;EXAMPLE: ; IDL>a = [[2,3],[4,5],[6,7]] ; IDL>print, a ; 2 3 ; 4 5 ; 6 7 ; IDL>print, WRESHAPE(a, 2, 3) ; 2.00000 6.00000 5.00000 ; 4.00000 3.00000 7.00000 ; ;MODIFICATION HISTORY: ; copyright (c) Amara Graps 1995, 1996. ;- t = SIZE(A) IF t(0) EQ 1 THEN BEGIN ;1D array shape = FLTARR(N_ELEMENTS(A)) shape(0) = A(*) ENDIF ELSE BEGIN ;2D array numcol = t(1) ;number of columns numrow=t(2) ;number of rows all = WCOMBINE(A) ;make into one row CASE 1 OF m*n EQ numrow*numcol: BEGIN ;valid shape=FLTARR(n,m) ;Step through the cols FOR i = 0, n-1 DO BEGIN ;Step through the rows FOR j = 0, m-1 DO BEGIN shape(i,j) = all(0,i*m+j) END ;j END ;i END ;valid # of rows and cols ELSE: PRINT, 'Error: m*n not eq to nrows*ncols!' ENDCASE ;checking values of m & n ENDELSE RETURN, shape END ;of function WRESHAPE ;************************************************