%% This is an open source software

function [distmat,alldistmat] = simmat_20161114_partial(matchedpatterninfo,matchedpatterninfo_ref, oz)

%   This function creates a simmilarity matrix for two or symbolic series 
  
%   Input paremeters
%   matchedpatterns --- patterns and probs for all series that are macthed 
%   refin : column vector of indices as the reference category to be
%   compared ( added on 20170223 by Akbilgic)
%   oz          --- structure
%   Output paremeter
%   alldistmat  --- simmatrix at each level
%   distmat    --- aggregated simmatrix

%   Akbilgic 20150901

%% set parameters and initialize
nref=size(matchedpatterninfo_ref,1); % size of the reference category
nseries=size(matchedpatterninfo,1);

distmat=zeros(nseries,nref);
alldistmat=zeros(nseries,nref,oz.maxmem);
%% calculate simmilarities between symbolic series
% 'simmat_partial'

%% temporary matrices for paralel pool

parind=zeros(nseries*nref,3);% burada parindin 2.sutunu satir, 3.sutunu sutu nnumarasini tutacak. ilk sutu nise sadece indeks amacli
sayac=0;
for i=1:nseries
    for j=1:nref
        sayac=sayac+1;
        parind(sayac,:)=[sayac,i,j];
    end
end
parind1=parind(:,1);
parind2=parind(:,2);
parind3=parind(:,3);
distmat_temp=zeros(nseries*nref,1);
alldistmat_temp=zeros(nseries*nref,oz.maxmem);
% myCluster = parcluster('local');
% mypool=parpool(myCluster.NumWorkers);
%
%for i=1:nseries-1
%    for j=(i+1):nseries
for i=1:size(parind,1)
    %i
%       [distmat_temp(i,1),alldistmat_temp(i,:)]=class_dist2(lagpatterns(parind(i,2),:),lagprobs(parind(i,2),:),lagpatterns(parind(i,3),:),lagprobs(parind(i,3),:),oz);
%         parind2(i),parind3(i);
       [distmat_temp(i,1), xxx] = dist_ptm_20161108( matchedpatterninfo{parind2(i),:},matchedpatterninfo_ref{parind3(i),:},oz ) ;
       alldistmat_temp(i,:)=xxx';
%        [distmat(i,j), alldistmat(i,j,:)] = dist_ptm_20161108(matchedpatterninfo{i,1},matchedpatterninfo{j,1},oz )  ;      
%        distmat(j,i)=distmat(i,j);
%        alldistmat(j,i,:)=alldistmat(i,j,:);
%    end    
end
% delete(mypool)

%%%%%%


%parfor i=1:size(parind,1)
%    [distmat_temp(i,1),alldistmat_temp(i,:)]=class_dist2(lagpatterns(parind(i,2),:),lagprobs(parind(i,2),:),lagpatterns(parind(i,3),:),lagprobs(parind(i,3),:),oz);
%end
sayac=0;
for i=1:nseries
    for j=1:nref
        sayac=sayac+1;
        distmat(i,j)=distmat_temp(sayac,1);
%        distmat(j,i)=distmat(i,j);
        alldistmat(i,j,:)=alldistmat_temp(sayac,:);
%        alldistmat(j,i,:)=alldistmat(i,j,:);
    end
       
end





% Copyright J. Andrew Howe & Oguz Akbilgic 2015
% this code may be freely used, modified, and distributed (at no charge)
% as long as this footer remains unaltered
% Updated by Akbilgic 20161107
% Updated by Akbilgic 20170223
 