I am reading the same binary file into Python and Matlab and putting it into a matrix. When I take the norm of this matrix, I get different results.
I am using the respective smatload function to load the binary file.
Python:
def smatload(filename):
#print 'opening: ', filename
f = open(filename, 'rb')
m = np.fromfile(f,'q',1)
n = np.fromfile(f,'q',1)
nnz = np.fromfile(f,'q',1)
print 'reading %d x %d with %d non-zeros' % (m,n,nnz)
S = np.fromfile(f,'d',3*nnz)
f.close()
S = S.reshape((nnz,3))
rows = S[:,0].astype(int) - 1
cols = S[:,1].astype(int) - 1
vals = S[:,2]
return csr_matrix((vals,(rows,cols)),shape=(m,n))
Matlab:
function [A] = smatload(filename)
fid = fopen(filename,'r');
if( fid == -1 )
disp(sprintf('Error: Unable to open file [%s], fid=%d\n',filename,fid));
A = [-1];
fclose(fid);
return;
end
m = fread(fid,[1 1],'uint64');
n = fread(fid,[1 1],'uint64');
nnz = fread(fid,[1 1],'uint64');
fprintf('Reading %d x %d with %d non-zeros\n',m,n,nnz);
S = fread(fid,[3 nnz],'double');
fclose(fid);
A = sparse(S(1,:)',S(2,:)',S(3,:)',m,n);
The results that I get for the norms of the returned matrices are
Matlab: norm(A.'fro') = 0.018317077159881
Python: np.linalg.norm(A) = 0.018317077159760
I have confirmed that they are both reading the correct number of values (6590x7126 matrix, 122526 non-zeros), and that I am using the same norm for both (frobenius).
Any ideas as to what might cause this?
scipy.io.loadmat
, and whether the results are the same. – hpaulj