Home > cellorganizer > utilities > getMeanModel.m

getMeanModel

PURPOSE ^

Compute (weighted) average model

SYNOPSIS ^

function [ mean_model ] = getMeanModel( models, weights )

DESCRIPTION ^

Compute (weighted) average model

gj Aug 30, 2013

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ mean_model ] = getMeanModel( models, weights )
0002 %Compute (weighted) average model
0003 %
0004 %gj Aug 30, 2013
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     %Create a new struct
0018     mean_struct = struct;
0019     
0020     %Get all the fields
0021     fnames = fieldnames(struct_parents{1});
0022     
0023     %For each field
0024     for i = 1:length(fnames)
0025         %retreive the value for each field
0026         vals = arrayfun(@(x) eval(['struct_parents{' num2str(x) '}.' fnames{i}]), 1:length(struct_parents), 'uniformoutput', false);
0027 
0028         if isstruct(vals{1})
0029             %%%RECURSIVE%%%
0030             mval = meanstruct(vals, weights);
0031         elseif isstr(vals{1})
0032             %if its a string we assume all the strings are the same
0033             mval = vals{1};
0034         else
0035             %take the weighted average
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

Generated on Sun 29-Sep-2013 18:44:06 by m2html © 2005