ParametersRun500
dataset_positions = [{2,0}; {2,1}; {2,2}; {2,3}; {2,4}; {4,0}; {4,2}; {4,3}; {4,4}; {4,5}; {5,0}; {5,1}; {5,2}; {5,3}; {6,0}; {6,1}; {6,2}; {6,3}; {6,4}; {6,5}; {7,0}; {7,1}; {7,2}; {7,3}; {7,4}; {7,5}; {10,0}; {10,1}; {10,2}; {10,3}; {10,5}];

moving_run_number = 500;

clear third_angles;
clear second_angles;

clear second_axes_project;
clear second_axes_normed;

clear first_moments;
clear second_moments;
clear third_moments;

clear third_axes_project;
clear third_axes_normed;

clear all_moments;

clear avg_first_moment;
clear avg_second_moment;
clear avg_third_moment;
clear avg_first_angle;
clear avg_second_angle;
clear avg_third angle;

for i = 1:size(dataset_positions,1)

    dataset = dataset_positions{i,1};
    position = dataset_positions{i,2};
    first_stack = calculated_stacks{dataset,position+1}(1);
    last_stack = calculated_stacks{dataset,position+1}(2);
    
    moving_start_time = moving_start_times{dataset,position+1};
    moving_moments_time = moments_times{dataset,position+1};
        
    if (dataset~= 2)&&(dataset~=4)
        moving_dataset_directory =  ['L:\seg2 5-31\Set ' num2str(dataset) '\Stacks\Position ' num2str(position) '\resized images'];
    elseif (dataset==2)&&(position == 4)
        moving_dataset_directory = 'L:\seg2 5-31\2-4 (cp from E drive 5-30)\Position 4\resized images\';
        moving_start_time = '15-Nov-2015 12.12.47';
    else
        moving_dataset_directory =  ['K:\Laptop F drive copy\new reg 4-8 classifier 6-13-15\' num2str(dataset) '-' num2str(position)];
    end
    
    cd(moving_dataset_directory)
    cd(['Run ' num2str(moving_run_number)]);    
    load(['momout_' num2str(dataset) '_' num2str(position) '_r' num2str(moving_run_number) '. ' moving_start_time '.mat']);
    
    these_princ = momout.princ_over_time;
    
    % need a 2D projection of the off-axes.
    
    principal_axes_over_time = momout.princ_over_time;
    these_moments = momout.d_array;
    
    these_num_axes = size(principal_axes_over_time,1);
    num_axes(i) = these_num_axes;
    
    principal_axis = mean(squeeze(principal_axes_over_time(:,:,1)),1);
    principal_axis = principal_axis/norm(principal_axis);
    
    first_axes = principal_axes_over_time(:,:,1);
    second_axes = principal_axes_over_time(:,:,2);
    third_axes = principal_axes_over_time(:,:,3);
    
    
clear second_axes_project;
clear second_axes_normed;

clear third_axes_project;
clear third_axes_normed;

all_moments(i,1:these_num_axes,:) = these_moments;
    
    for index = 1:these_num_axes

%     first_axes_project(index,:) = first_axes(index,:) - dot(first_axes(index,:),principal_axis);
%     first_axes_normed(index,:) = first_axes_project(index,:)./norm(first_axes_project(index,:));
    
        
    second_axes_project(index,:) = second_axes(index,:) - dot(second_axes(index,:),principal_axis);
    second_axes_normed(index,:) = second_axes_project(index,:)./norm(second_axes_project(index,:));
    
    third_axes_project(index,:) = third_axes(index,:) - dot(third_axes(index,:),principal_axis);
    third_axes_normed(index,:) = third_axes_project(index,:)./norm(third_axes_project(index,:));
    
    
%     first_angle = real(acos(dot(first_axes_normed(index,:),first_axes_normed(1,:))));
    first_angle = real(acos(dot(first_axes(index,:),principal_axis)));

    first_angle = min(first_angle, pi-first_angle);
    first_angles(i,index) = first_angle;   
    
%     second_angle = real(acos(dot(second_axes_normed(index,:),second_axes_normed(1,:))));

    if second_axes_normed(index,1)<0
        second_axes_normed(index,:) = -1*second_axes_normed(index,:);
        third_axes_normed(index,:) = -1*third_axes_normed(index,:);
    end
    
    second_axis = mean(second_axes_normed,1);
    second_axis = second_axis./norm(second_axis);
    
    second_angle = real(acos(dot(second_axes_normed(index,:),second_axis)));
    second_angle = min(second_angle, pi-second_angle);
    second_angles(i,index) = second_angle;
    
    third_axis = mean(third_axes_normed,1);
    third_axis = third_axis./norm(third_axis);
    
    third_angle = real(acos(dot(third_axes_normed(index,:),third_axis)));
    third_angle = min(third_angle, pi-third_angle);
    third_angles(i,index) = third_angle;
    
    
    end
    
end


for i = 1:size(dataset_positions,1)

avg_first_angle(i) = mean(first_angles(i,1:num_axes(i)));
avg_second_angle(i) = mean(second_angles(i,1:num_axes(i)));
avg_third_angle(i) = mean(third_angles(i,1:num_axes(i)));

avg_first_moment(i) = mean(all_moments(i,1:num_axes(i),1));
avg_second_moment(i) = mean(all_moments(i,1:num_axes(i),2));
avg_third_moment(i) = mean(all_moments(i,1:num_axes(i),3));

var_first_moment(i) = var(all_moments(i,1:num_axes(i),1));
var_second_moment(i) = var(all_moments(i,1:num_axes(i),2));
var_third_moment(i) = var(all_moments(i,1:num_axes(i),3));

avg_moments_32ratio(i) = mean(all_moments(i,1:num_axes(i),3)./all_moments(i,1:num_axes(i),2));
avg_moments_21ratio(i) = mean(all_moments(i,1:num_axes(i),2)./all_moments(i,1:num_axes(i),1));
avg_moments_31ratio(i) = mean(all_moments(i,1:num_axes(i),3)./all_moments(i,1:num_axes(i),1));

end

std_first_moment = sqrt(sum(var_first_moment));
std_second_moment = sqrt(sum(var_second_moment));
std_third_moment = sqrt(sum(var_third_moment));