1 |
edhill |
1.1 |
% EXTRACT - Extract a section from an input vector which contains an |
2 |
|
|
% n-dimensional array in a linear series. |
3 |
|
|
% Usage: out = Extract( InputVector, section ) |
4 |
|
|
% InputVector is of form |
5 |
|
|
% [ NumberDimensions Dim1 Dim2 ... DimN data ] |
6 |
|
|
% section is of form |
7 |
|
|
% [ Dim1Min Dim1Max Dim2Min Dim2Max ... DimNMin DimNMax ] |
8 |
|
|
% |
9 |
|
|
function outArr = Extract( inArr, subSection ) |
10 |
|
|
if nargin ~= 2 |
11 |
|
|
fprintf(2,'**ERROR** wrong number of arguments.\n') |
12 |
|
|
fprintf(2,'Usage: a = Slice ( b, [ xmin xmax ymin ymax ... ]\n') |
13 |
|
|
return |
14 |
|
|
end |
15 |
|
|
if size(subSection,1) ~= 1 |
16 |
|
|
fprintf(2,'**ERROR** sub-section incorrectly specified.\n') |
17 |
|
|
fprintf(2,'Usage: a = Slice ( b, [ xmin xmax ymin ymax ... ]\n') |
18 |
|
|
return |
19 |
|
|
end |
20 |
|
|
if inArr(1)*2 ~= size(subSection,2) |
21 |
|
|
fprintf(2,'**ERROR** sub-section has wrong number of elments.\n') |
22 |
|
|
fprintf(2,'Usage: a = Slice ( b, [ xmin xmax ymin ymax ... ]\n') |
23 |
|
|
return |
24 |
|
|
end |
25 |
|
|
% |
26 |
|
|
% Get dimensions of data array help in input vector. |
27 |
|
|
nD=inArr(1); |
28 |
|
|
Ds=inArr(2:nD+1); |
29 |
|
|
for i=1:nD |
30 |
|
|
s1(i)=subSection((i-1)*2+1); s2(i)=subSection((i-1)*2+2); |
31 |
|
|
end |
32 |
|
|
inStep(1)=1; |
33 |
|
|
for i=2:nD |
34 |
|
|
inStep(i)=inStep(i-1)*Ds(i-1); |
35 |
|
|
end |
36 |
|
|
nC=1; |
37 |
|
|
for i =1:nD |
38 |
|
|
nC=nC*(subSection((i-1)*2+2)-subSection((i-1)*2+1)+1); |
39 |
|
|
end |
40 |
|
|
wk=zeros(nC,nD); |
41 |
|
|
% |
42 |
|
|
% Build list of coordinates to extract. |
43 |
|
|
for i=1:nD |
44 |
|
|
ss=subSection((i-1)*2+1); se=subSection((i-1)*2+2); |
45 |
|
|
if i == 1 lCycle = 1; |
46 |
|
|
else lCycle = prod((s2(1:i-1)-s1(1:i-1)+1)); |
47 |
|
|
end |
48 |
|
|
if i < nD nRep = prod((s2(i+1:nD)-s1(i+1:nD)+1)); |
49 |
|
|
else nRep = 1; |
50 |
|
|
end |
51 |
|
|
blk=reshape(([ss:se]'*(zeros(1,lCycle)+1))',lCycle*(se-ss+1),1); |
52 |
|
|
wk(:,i)=reshape(blk*(zeros(1,nRep)+1),nRep*lCycle*(se-ss+1),1); |
53 |
|
|
end |
54 |
|
|
% |
55 |
|
|
% Translate coordinate list into offsets along vector. |
56 |
|
|
iS=[0,inStep(2:nD)]; |
57 |
|
|
wk(:,1)=1+nD+wk(:,1)+(wk-1)*iS'; |
58 |
|
|
% |
59 |
|
|
% Extract selected locations. |
60 |
|
|
outArr=inArr(wk(:,1)'); |
61 |
|
|
return |