%Author: ankur kumar
%img.mat is saved by density.m and used in this program
%input: density plot indices of the frequency plot passing a threshold and auxiliary matrix
%output: datapoints from the dataset that have the highest density, the
%retreived dataset, it can be used for Grand Tour

function main()
clear all;
load pollen.csv; %the dataset used
load img.mat; %from density.m

%A is auxiliary matrix, nc and nr are index values of frequency matrix
%satisfying a threshold. The auxiliary matrix is the same size as the
%pollen dataset matrix
%find where nc and nr, the index values of the frequency matrix,
%occur in the auxiliary matrix
entrynum = unique(findrows(nc,nr,A));

size(entrynum)
%found entry-number, go to pollen data and store the entry into a matrix
newmat = pollen(entrynum,:); %retreived dataset, can be used for Grand Tour
%ready for grand tour
save gtdata.mat
m = length(pollen(:,1)); %number of entries in dataset
n = length(pollen(1,:))-1; %number of dimensions in dataset
map = gray(m);
figure(1)
hold on
%graph the original pollen dataset
for i=1:m
    plot(pollen(i,[1:5]),'color',map(i,:));
end
%graph the retreived points in red from the dataset
for i=1:length(newmat(:,1))
    plot(newmat(i,[1:5]),'r');
end

%the axes for parallel coords
axisx = [1;n];
axisy = [0;0];
label = [{'r'},{'n'},{'c'},{'w'},{'d'}];
for i = 1:n
    axisx = [axisx,[i;i]];
    axisy = [axisy,[-40;40]];
end
line(axisx, axisy, 'color', 'b');           %draw the axes
set(gca, 'xtick',[1:1:n]);
set(gca, 'xticklabel', label);
hold off

%finds the rows where vecx occurs in mat, then finds if those rows contain
%any value of vecy, if it does, then store that row number because it is a
%point that has been graphed in the high density region
function rows = findrows(vecx,vecy,mat)
rows=[];
vecx = unique(vecx);
vecy = unique(vecy);
for i=1:length(vecx)
        %only need to keep track of rows, since it is parallel coordinates
        temp = vecx(i);
        [ans1,ans2] = find(mat==temp); %got rows
        %check if the row has the value of any vecy
        for k=1:length(ans1)
            trow = mat(ans1(k),:);
            for j=1:length(trow)
                ans3 = find(vecy==trow(j)); %find if any value in trow is in vecy
                if(length(ans3)~=0)
                    rows=[rows;ans1(k)]; %value exists in vecy, store row num of mat
                    break; %break out of this loop
                end
            end
        end 
end
rows;

