%PAJEK Converts a Matlab weight matrix into a Pajek input file.
% PAJEK(MAT,'file_name') writes the network described by the
% square matrix MAT to a Pajek input file called 'file_name.net'
% The network will be weighted and directed.
%
% For example,
%
% pajek(W,'Network1') eg.,
% >> Matrix1=nearest(10,4);
% >> pajek(Matrix1,lj)
%
% creates a file called 'Network1.net' in the present working
% directory from the network descibed by W.
% Ben Skellett, 15/10/02
% Revised 28/07/03
function pajek(mat,file)
if size(mat,1)~=size(mat,2)
error('Matrix is not square');
end
if ~ischar(file)
error('Specify file name as a string');
end
vertices=1:size(mat,1);
[i,j,v]=find(mat);
arcs=[i'; j'; v'];
fid=fopen([file,'.net'],'w');
fprintf(fid,'*Vertices %g \r',size(mat,1));
fprintf(fid,'%g "v%g" \r',[vertices; vertices]);
fprintf(fid,'*Arcs \r');
fprintf(fid,'%g %g %g\r',arcs);
fclose(fid);