function OutPut_ARRAY=Levels_Array(CURRENT_FLS)
% function OutPut_ARRAY=Levels_Array(Lev_Struct)
% function OutPut_ARRAY=Levels_Array(CURRENT_FLS)
%
% TAKES a sturcture, where each field gives the levels of one factor. 
% RETURNS an N dimensional array, where the first N-1 dimensions are given
% by the number of levels of each factor, and the lastis given by the
% number of factors.
%
% Adding along the N_factors+1 dimension, and then adding the Grand Mean,
% gives the best estimates of the missing values, given the factor levels. 


field_names=fields(CURRENT_FLS);
N_factors=length(field_names);

N_levels=NaN(1, N_factors);

for jj=1:1:N_factors
    N_levels(jj)=length(eval(strcat(['CURRENT_FLS.', field_names{jj}])));
end


OutPut_ARRAY=[];

for jj=1:1:N_factors
    fac_arr=permute(NaN(N_levels), [jj, setdiff([1:1:N_factors], jj)]);
    %fill it
    levelers=eval(strcat(['CURRENT_FLS.', field_names{jj}]));
    for kk=1:1:N_levels(jj)
        fac_arr(kk,:,:)=levelers(kk);
    end
    %undo the permutation:
    [vals, ord]=sort([jj, setdiff([1:1:N_factors], jj)]);
    fac_arr=permute(fac_arr, ord);
    OutPut_ARRAY=cat(N_factors+1,OutPut_ARRAY,fac_arr);
end


