function [W V txt]=formatVocab(voc,D,K,mode) %WORDBYDIST output words sorted by distance matrix D. % [W V TXT] = FORMATVOCAB(VOC,D,K) % Lorem ipsum. % % Arguments: % VOC Vocabulary % D All pairs of words numeric relationship % K Number of columns to print % MODE {'ascend'} % 'descend' % % Returns: % W Sorted words % V Sorted distances % TXT Formatted text output % % Authors: % Joshua V. Dillon; Nov. 6, 2006; jvdillon AAT purdue DDOT edu if nargin==3 mode = 'ascend'; elseif nargin==4 & (strcmp(mode,'ascend')==0 | strcmp(mode,'descend')==0) % do nothing else error('invalid arguments'); end if issparse(D) D = full(D); end %K = 4; [V J]=sort(D,2,mode); V = V(:,1:K); W = voc(J(:,1:K)); [ignore I]=sort(V,1,mode); V = V(I(:,1),:); W = W(I(:,1),:); a=[voc(I(:,1)) W num2cell(V)]'; % need to intermingle W and V to form pairs Imerge = [1 reshape(reshape(1:(2*K),K,2)',1,2*K)+1]; a=a(Imerge,:); wrdfmt = ['%' num2str(size(char(voc),2)) 's']; fmt = [wrdfmt ' | ' repmat([wrdfmt ' % 2.4e '],1,K) '\n']; txt = sprintf(fmt,a{:}); %fprintf(txt);