function M=mtxread(mtxfile,mtxformat)
% usage: M=mtxread(mtxfile,mtxformat)
% This function reads the Matrix Market file mtxfile into
% a matrix M in dense representation.
% mtxformat :
%     'G' : general
%     'S' : symmetric
% JBC, Fev 2000
fid = fopen(mtxfile,'r');
first_line=fgets(fid);
[iv,siv]=fscanf(fid,'%f',3);
n=iv(1);m=iv(2); M=zeros(n,m);
if nargin < 2 mtxformat='Ge';end
if (lower(mtxformat(1))=='g')
  while ~feof(fid)
    [iv,siv]=fscanf(fid,'%f',3);
    if (siv==3)
       M(iv(1),iv(2))=iv(3);
    end
  end
elseif (lower(mtxformat(1))=='s')
  while ~feof(fid)
    [iv,siv]=fscanf(fid,'%f',3);
    if (siv==3)
       M(iv(1),iv(2))=iv(3);
       if (iv(1) > iv(2))
         M(iv(2),iv(1))=iv(3);
       end
    end
  end
else
  error('Format not recognized.')
end
fclose(fid);
return
