function net = mixmlpunpak(net, w)
%mixmlpunpak Separates weight vector into weight and bias matrices for a gated experts model. 
%
%       NET = MIXMLPUNPAK(NET, W) takes a gated experts data structure
%       NET and  a weight vector W, and returns a network data structure 
%       identical to the input network, except that the weights and biases
%       of the expert networks and of the gating network (which are MLPs) 
%       have been set to the corresponding elements of W.
%
%       See also
%       mlpunpak, mixmlp, mixmlppak, mixmlpfwd, mixmlperr, mixmlpgrad
%       
%
%       Copyright: Perry Moerland


% Check arguments for consistency
errstring = consist(net, 'mixmlp');
if ~isempty(errstring);
  error(errstring);
end

if net.nwts ~= length(w)
  error('Invalid weight vector length')
end

nexp = net.nexp;

index1 = 1;
for i=1:nexp
 index2 = (index1+net.exp(i).nwts)-1;
 net.exp(i) = mlpunpak(net.exp(i),w(index1:index2));
 index1 = index2+1;
end;

index2 = (index1+net.gate.nwts)-1;
net.gate = mlpunpak(net.gate,w(index1:index2));