0001 function [ mean_model ] = getMeanModel( models, weights )
0002 
0003 
0004 
0005 
0006 if ~exist('weights', 'var')
0007     weights = ones(size(models)) / length(models);
0008 end
0009 
0010 weights = weights ./ sum(weights);
0011 
0012 mean_model = meanstruct(models, weights);
0013 
0014 end
0015 
0016 function [mean_struct] = meanstruct(struct_parents, weights)
0017     
0018     mean_struct = struct;
0019     
0020     
0021     fnames = fieldnames(struct_parents{1});
0022     
0023     
0024     for i = 1:length(fnames)
0025         
0026         vals = arrayfun(@(x) eval(['struct_parents{' num2str(x) '}.' fnames{i}]), 1:length(struct_parents), 'uniformoutput', false);
0027 
0028         if isstruct(vals{1})
0029             
0030             mval = meanstruct(vals, weights);
0031         elseif isstr(vals{1})
0032             
0033             mval = vals{1};
0034         else
0035             
0036             wval = cellfun(@(x,y) x.*y, vals, num2cell(weights), 'uniformoutput', false);
0037             catdim = ndims(wval{1})+1;
0038             wcat = cat(catdim, wval{:});
0039             mval = sum(wcat,catdim); 
0040         end
0041         
0042         eval(['mean_struct.' fnames{i} ' = mval;']);
0043     end 
0044 end