%% READ in and get the 3 percentiles of the imputed values:
% Load the samples of the parameters  - get the number of samples, burnin,
% etc.

load PARS_ARRAY
load Data_INPUT
% get the number of samples
N_Its_ALL=length([PARS_ARRAY.GM]);
burnin=min(600, 0.5*N_Its_ALL);
N_Its=N_Its_ALL-burnin;

%% start loading at burnin+1


Samples_of_Factor_Array=[];
load Imputations/Imputed_Values_Step1
N_factors=ndims(Current_Imputed_Values);
clear Current_Imputed_Values
for KK=1:1:N_Its        
    load(['Imputations/Imputed_Values_Step', num2str(KK+burnin)])
    Samples_of_Factor_Array=cat(N_factors+1,Samples_of_Factor_Array,Current_Imputed_Values);
    clear Current_Imputed_Values
    KK
end

%Save the giant array:
save Imputations/Samples_of_Factor_Array Samples_of_Factor_Array -v7.3

%% Get the three parameters:
pers3=round([0.05, 0.50, 0.95]*N_Its);

%S_Samples_of_Factor_Array=sort(permute(Samples_of_Factor_Array, [N_factors+1, setdiff([1:1:N_factors+1], N_factors+1)]), 1);
S_Samples_of_Factor_Array=sort(Samples_of_Factor_Array, N_factors+1);


Temper='S_Samples_of_Factor_Array(';
for KK=1:1:N_factors
    Temper=[Temper, ':,'];
end
Temper=[Temper, 'pers3)'];

Samples_of_Factor_Array_pers3=eval(Temper);

%Save the with the imputations and in the main directory:
save Imputations/Samples_of_Factor_Array_pers3 Samples_of_Factor_Array_pers3 -v7.3
save Samples_of_Factor_Array_pers3 Samples_of_Factor_Array_pers3