1% Pack a series of variables into a vector under the control of a mask. 2% 3% x = statevector_pack(s,var1, var2, ...) 4% 5% Pack the different variables var1, var2, ... into the vector x. 6% Only sea grid points are retained. 7% 8% Input: 9% s: structure generated by statevector_init. 10% var1, var2,...: variables to pack (with the same shape as the corresponding masks). 11% 12% Output: 13% x: vector of the packed elements. The size of this vector is the number of elements of all masks equal to 1. 14% 15% Notes: 16% If var1, var2, ... have an additional trailing dimension, then this dimension is assumed 17% to represent the different ensemble members. In this case x is a matrix and its last dimension 18% is the number of ensemble members. 19 20% Author: Alexander Barth, 2009 <a.barth@ulg.ac.be> 21% License: GPL 2 or later 22 23function x = statevector_pack(varargin) 24 25s = varargin{1}; 26 27if isvector(varargin{2}) && isvector(s.mask{1}) 28 % handle odd case when varargin{1} is a vector of size n x 1 and 29 % s.mask{1} is 1 x n 30 k = 1; 31else 32 k = size(varargin{2},my_ndims(s.mask{1})+1); 33end 34 35x = zeros(s.n,k); 36 37for i=1:s.nvar 38 tmp = reshape(varargin{i+1},s.numels_all(i),k); 39 40 ind = find(s.mask{i}==1); 41 42 x(s.ind(i)+1:s.ind(i+1),:) = tmp(ind,:); 43end 44 45 46function d = my_ndims(v) 47 if isvector(v) 48 d = 1; 49 else 50 d = ndims(v); 51 end 52 53 54 55 56 57 58 59% Copyright (C) 2009 Alexander Barth <a.barth@ulg.ac.be> 60% 61% This program is free software; you can redistribute it and/or modify 62% it under the terms of the GNU General Public License as published by 63% the Free Software Foundation; either version 2 of the License, or 64% (at your option) any later version. 65% 66% This program is distributed in the hope that it will be useful, 67% but WITHOUT ANY WARRANTY; without even the implied warranty of 68% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 69% GNU General Public License for more details. 70% 71% You should have received a copy of the GNU General Public License 72% along with this program; If not, see <http://www.gnu.org/licenses/>. 73 74 75% LocalWords: statevector init GPL 76