function [unique] = profilePermutations(pref)

[~, nProfiles, nCitizens] = size(pref);

     
%% Get all relabelings    
    
% 123 -> 132

pref132 = zeros(size(pref));

for i = 1 : numel(pref)
    if pref(i) == 1
        pref132(i) = 1;
    elseif pref(i) == 2
        pref132(i) = 3;
    else
        pref132(i) = 2;
    end
end

% 123 -> 213

pref213 = zeros(size(pref));

for i = 1 : numel(pref)
    if pref(i) == 1
        pref213(i) = 2;
    elseif pref(i) == 2
        pref213(i) = 1;
    else
        pref213(i) = 3;
    end
end

% 123 -> 231

pref231 = zeros(size(pref));

for i = 1 : numel(pref)
    if pref(i) == 1
        pref231(i) = 2;
    elseif pref(i) == 2
        pref231(i) = 3;
    else
        pref231(i) = 1;
    end
end

% 123 -> 312

pref312 = zeros(size(pref));

for i = 1 : numel(pref)
    if pref(i) == 1
        pref312(i) = 3;
    elseif pref(i) == 2
        pref312(i) = 1;
    else
        pref312(i) = 2;
    end
end

% 123 -> 321

pref321 = zeros(size(pref));

for i = 1 : numel(pref)
    if pref(i) == 1
        pref321(i) = 3;
    elseif pref(i) == 2
        pref321(i) = 2;
    else
        pref321(i) = 1;
    end
end


prefRelabeled = cat(5, pref, pref132, pref213, pref231, pref312, pref321);
[~, ~, ~, ~, nRelabelings] = size(prefRelabeled);



%% Get all reorderings of people

voterIndex = 1:nCitizens;
voterIndex = perms(voterIndex);

[nPerms, ~] = size(voterIndex);

%nPerms = 5;

prefRelabeled = repmat(prefRelabeled, [1 1 1 nPerms nRelabelings]);

for permutation = 1 : nPerms
    for relabeling = 1 : nRelabelings
        prefRelabeled(:,:,:,permutation, relabeling) = prefRelabeled(:,:,voterIndex(permutation, :), permutation, relabeling);
    end
end



%% Check for equivalent preference profiles

% Once a profile is found equivalent to another one, tag the first and its 
% equivalence class, then stop comparing subsequent profiles to those. 

out = zeros(nProfiles, 3);

for profile = 1 : nProfiles
    disp(profile)
    for comparisonProfile = 1 : nProfiles
        if comparisonProfile ~= profile
            for permutation = 1 : nPerms
                for relabeling = 1 : nRelabelings
                    if min(squeeze(min(pref(:,profile,:) == prefRelabeled(:,comparisonProfile,:,permutation, relabeling)))) == 1
                        out(profile, :) = [comparisonProfile relabeling permutation];
                        prefRelabeled(:,profile,:,:,:) = zeros(size(prefRelabeled(:,profile,:,:,:)));
                    end
                end
            end
        end
    end
end

unique = out(:,1) == 0;


