source: trunk/SRC/Matrix/extrac2.pro @ 378

Last change on this file since 378 was 373, checked in by pinsard, 16 years ago

improvements of headers (examples and results)

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1;+
2;
3; @file_comments
4; extraction of subdomains of matrices;
5; Even if the subdomain is "pierced" (see the example)
6; By default, IDL can make extractions of subdomain:
7;
8;   IDL> a=indgen(5,5)
9;   IDL> print, a
10;             0       1       2       3       4
11;             5       6       7       8       9
12;            10      11      12      13      14
13;            15      16      17      18      19
14;            20      21      22      23      24
15;   IDL> print, a[[0,2],3]
16;            15      17
17;   IDL> print, a[[0,2],*]
18;             0       2
19;             5       7
20;            10      12
21;            15      17
22;            20      22
23; but
24;   IDL> print, a[[0,2],[3,4]]
25;            15      22
26; while
27;   IDL> print, extrac2(a,[0,2],[3,4])
28;            15      17
29;            20      22
30;
31; @categories
32; Utilities
33;
34; @param array {in}{required}
35; a 1d, 2d, 3d or 4d input array
36;
37; @param index1 {in}{required}
38; can have 2 forms:
39;
40;  1) a vector containing indexes of lines we want to keep
41;  2) the string '*' if we want to keep all lines.
42;
43; @param index2 {in}{required}
44; the same thing that index1 but for dim 2.
45;
46; @param index3 {in}{required}
47; the same thing that index1 but for dim 3.
48;
49; @param index4 {in}{required}
50; the same thing that index1 but for dim 4.
51;
52; @returns
53; a matrix 1d, 2d, 3d or 4d extract from input array
54; -1 in case of mistake
55;
56; @restrictions
57;
58; @examples
59; I have a 2d matrix named A. I want extract a small intersection
60; matrix 2d of the line 2, 3 and 7 and of the column 0 and 1:
61;
62;   IDL> res=extrac2(A,[2,3,7],[0,1])
63;
64; other ex:
65;   IDL> a=[['a','b','c'],['d','e','f'],['g','h','i']]
66;   IDL> print, a
67; a b c
68; d e f
69; g h i
70;   IDL> print, extrac2(a,[0,2],[0,2])
71; a c
72; g i
73;
74; @history
75; Sebastien Masson (smasson\@lodyc.jussieu.fr)
76;  - 12/1/1999
77;  - 29/4/1999: correction of a bug and complement of the heading
78;
79; @version
80; $Id$
81;
82;-
83FUNCTION extrac2, array, index1, index2, index3, index4
84;
85  compile_opt idl2, strictarrsubs
86;
87   taille = size(array)
88;------------------------------------------------------------
89; test of the number of parameters
90; and of the nature of the index (for THE case 'x')
91;------------------------------------------------------------
92   if n_params() NE taille[0]+1 THEN $
93    return, report('we need as many indexes as the number of dimensions of the input array')
94   IF n_params() GE 5 THEN BEGIN
95      if size(index4,/type) EQ 7 then index4 = lindgen(taille[4]) $
96      ELSE index4 = long(index4)
97      nt = n_elements(index4)
98   ENDIF
99   IF n_params() GE 4 THEN BEGIN
100      if size(index3,/type) EQ 7 then index3 = lindgen(taille[3]) $
101      ELSE index3 = long(index3)
102      nz = n_elements(index3)
103   ENDIF
104   IF n_params() GE 3 THEN BEGIN
105      if size(index2,/type) EQ 7 then index2 = lindgen(taille[2]) $
106      ELSE index2 = long(index2)
107      ny = n_elements(index2)
108   ENDIF
109   IF n_params() GE 2 THEN BEGIN
110      if size(index1,/type) EQ 7 then index1 = lindgen(taille[1]) $
111      ELSE index1 = long(index1)
112      nx = n_elements(index1)
113   ENDIF
114
115;------------------------------------------------------------
116; construction of an array of indexes and of results following the size of array
117;------------------------------------------------------------
118  case taille[0] of
119      1:res = array[index1]
120      2:BEGIN
121         index = index1#replicate(1, ny)+taille[1]*replicate(1, nx)#index2
122         res = array[index]
123      END
124      3:BEGIN
125         index = index1#replicate(1, ny)+taille[1]*replicate(1, nx)#index2
126         index = temporary(index[*])#replicate(1, nz) $
127          +taille[1]*taille[2]*replicate(1, nx*ny)#index3
128         res = array[reform(index, nx, ny, nz, /over)]
129      END
130      4:BEGIN
131         index = index1#replicate(1, ny)+taille[1]*replicate(1, nx)#index2
132         index = temporary(index[*])#replicate(1, nz) $
133          +taille[1]*taille[2]*replicate(1, nx*ny)#index3
134         index = temporary(index[*])#replicate(1, nt) $
135          +taille[1]*taille[2]*taille[3]*replicate(1, nx*ny*nz)#index4
136         res = array[reform(index, nx, ny, nz, nz, /over)]
137      END
138   endcase
139
140
141;------------------------------------------------------------
142;------------------------------------------------------------
143   return, res
144end
Note: See TracBrowser for help on using the repository browser.