function process_corr_p_a(conff,nlin,ncol,listlinf,listcolf,tit); % % M-function for processing of confusion matrix % % Syntax process_corr_p_a(conff,nlin,ncol,listlinf,listcolf,tit); % % conf - filename of confusion matrix (text) % nlin - number of its lines % ncol - number of its columns % listlin - labels for lines (filename) % listcol - labels for columns (filename) % title - title for plot %disp(['>' conff '<']) ff=fopen(conff,'r') conf=fscanf(ff, '%f', [ncol nlin]); %columnwise %conf=fscanf(ff, '%f', [nlin ncol]); %columnwise fclose(ff); conf=conf'; %%% in columns and rows, the sums should be 1... disp('sum of cols'); sum (conf) % - not really ones ... disp('sum of rows'); sum (conf') % this is ok %%% look at max and min disp('min'); min(min(conf)) %... 0 disp('max'); max(max(conf)) %... 0.8063 %%% visualize it figure(1); colormap(gray); imagesc(1-conf) ylabel ('phonetic classes'); xlabel ('alisp units'); title('not sorted'); %%% should sort it % construct "ideal diagonal matrix" - allow only one 1 per column ratio=(nlin-1)/(ncol-1); pos_in_cols=round((0:ncol-1)*ratio)+1; idig=zeros(nlin,ncol); for i=1:ncol, idig(pos_in_cols(i), i)=1; end % see ideal matrix figure(2); colormap(gray); imagesc(1-idig) ylabel ('phonetic classes'); xlabel ('alisp units'); title('ideal'); %%% now sort - go from best to worst column in conf (best %%% means with the clearest maximum. %%% thanks to V. Sebesta for this idea !!! % sort maxima of columns: [dummy, imaxcol]=sort(max(conf)); % this is ascending, wants desc. imaxcol=imaxcol(ncol:-1:1); % create empty matrix and vector with new indices. 0 will mean that % the position if free newconf=zeros(nlin,ncol); newind=zeros(1,ncol); % go from the best maxima, and put the vector where it best coincides % with the ideal pseudo-diagonal for ii=1:ncol, col_to_put=conf(:,imaxcol(ii)); % retrieve free positions free_pos=find(newind==0); % compute euclidian distance of vector with all columns on free % positions aux=idig(:,free_pos); eucl=sqrt(sum( (col_to_put*ones(1,length(free_pos))-aux).^2 )); % put the vector where we've minimal eucl distance [dummy, mini] = min(eucl); index_to_put=free_pos(mini); newconf(:, index_to_put) = col_to_put; newind(index_to_put) = ii; end %%% visualize sorted matrix figure(3); colormap(gray); imagesc(1-newconf); ylabel ('phonetic classes'); xlabel ('alisp units'); title(tit); %%% should put labels on axes. % read the two lists: list_lin=[]; ff=fopen(listlinf,'r'); for ii=1:nlin, str=fscanf(ff,'%s',[1 1]); if ii==1, list_lin=str; else list_lin=str2mat(list_lin, str); end end fclose(ff); list_col=[]; ff=fopen(listcolf,'r'); for ii=1:ncol, str=fscanf(ff,'%s',[1 1]); if ii==1, list_col=str; else list_col=str2mat(list_col, str); end end fclose(ff); %%% put them on axis h=gca; set (h,'fontsize',15); set (h,'fontweight','bold'); % with deletion of 1st char set(h,'xtick', 1:ncol); set (h,'xticklabel', list_col(imaxcol(newind),2)) % without deleteion of 1st char: %set(h,'xtick', 1:ncol); set (h,'xticklabel', list_col(imaxcol(newind),:)) set(h,'ytick', 1:nlin); set (h,'yticklabel', list_lin) %grid; % no - its not nice