%inputfilename must be a single column

function[output_matrix] = test_tableform(inputfilename,cluster,totalnoofclusters)

%Example
% [output_matrix] = test_tableform('ireses_gt.txt',cluster,3);

answer = load(inputfilename); 


min1 = min(answer(:,1));
max1 = max(answer(:,1));

if min1 == 0
for i = totalnoofclusters:-1:1
    index = find(answer(:,1) == max1);
    answer(index,1) = i;
    max1 = max1 - 1;
end
end

for i = 1:totalnoofclusters
    temp2 = 0;
    temp2 = find(answer == i);
    temp3(i) = size(temp2,1);
end

known_cluster = answer;
our_cluster = cluster;

permutation_matrix = perms(1:1:totalnoofclusters);

for i = 1:totalnoofclusters
    temp = find(known_cluster == i);
    for j = 1:size(permutation_matrix,1)
        correct = 0;
        temp1 = find(our_cluster == permutation_matrix(j,i));
        for k = 1:size(temp,1)
            match = [];
            match = find(temp1 == temp(k,:));
            if ~isempty(match)
                correct = correct + 1;
            end
        end
        check_matrix(j,i) = correct;
    end
end

for l = 1:totalnoofclusters
del = find(check_matrix(:,l) == 0);
check_matrix(del,:) = [];
permutation_matrix(del,:) = [];
end

for l = 1:totalnoofclusters
    ratio_matrix(:,l) = check_matrix(:,l)/temp3(l);
end

for l = 1:size(ratio_matrix,1)
    sum_matrix(l,:) = sum(ratio_matrix(l,:));
end

[maximum, I] = max(sum_matrix);

val = permutation_matrix(I,:);
check_matrix(I,:);

temp3;
permutation_matrix;
check_matrix;
ratio_matrix;

val1 = cluster;

b = 1;
for a = 1:totalnoofclusters
    ind = find(cluster == val(b));
    val1(ind,:) = a;
    b = b + 1;
end

output_matrix(totalnoofclusters, totalnoofclusters) = 0;

for i = 1:size(answer,1)
    if answer(i,:) == val1(i,:)
        output_matrix(answer(i,:),val1(i,:)) = output_matrix(answer(i,:),val1(i,:)) + 1;
    else
        output_matrix(answer(i,:),val1(i,:)) = output_matrix(answer(i,:),val1(i,:)) + 1;
    end
end

output_matrix'
%save('checkmatrix.txt','check_matrix','-ASCII');