/[MITgcm]/MITgcm_contrib/high_res_cube/matlab-grid-generator/bin/dimsum.m
ViewVC logotype

Annotation of /MITgcm_contrib/high_res_cube/matlab-grid-generator/bin/dimsum.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download)
Tue Nov 11 18:08:08 2003 UTC (23 years ago) by cnh
Branch point for: MAIN, initial
Initial revision

1 cnh 1.1 function ds = dimsum(A,d,varargin);
2     % Matrix multiply a vector using a multi-dimensional array.
3     % This allows weighted summation in a 3-D/4-D arrays over
4     % a single index.
5     %
6     % >> size(T)
7     % ans =
8     % 128 64 10 23
9     % >> size(dz)
10     % ans =
11     % 10 1
12     % >> vintegral=dimsum(T,dz,3);
13     % >> size(vintegral)
14     % ans =
15     % 128 64 23
16    
17     error(nargchk(2,3,nargin))
18    
19     if size(d,2) ~= 1, error('Second argument must be a vector'); end
20    
21     if nargin==3
22     dim=varargin{1};
23     else
24     if sum( size(A)==size(d,1) )==1
25     dim=find( size(A)==size(d,1));
26     else
27     error('One and only one dimension must match unless you specify the dimension to sum')
28     end
29     end
30    
31     if size(A,dim) ~= size(d,1)
32     error('Dimensions of matrix and vector do not match')
33     end
34    
35     dims=[size(A) 1];
36     nd=size(d,1);
37     ip=[dim:ndims(A)+1 1:dim-1];
38     A=permute(A,ip);
39     A=A(1:nd,:);
40     ds=A'*d;
41    
42     dims(dim)=1;
43     dims=dims(ip);
44     ds=reshape(ds,dims);
45     ds=squeeze(ipermute(ds,ip));

  ViewVC Help
Powered by ViewVC 1.1.22