I am attempting to convert a matlab script to python. The script involves writing data to a binary file. I have noticed differences between the files, specifically when writing matrices/numpy arrays. When writing other variable types (int, string etc) the files are identical as desired.
Matlab code:
fid = fopen("test2.txt", "wb");
a = [[1 2];[3 4]];
fwrite(fid, a, "float64");
fclose(fid);
Matlab result (as shown by notepad):
ð? @ @ @
Python code:
import numpy as np
with open("test2.txt", "wb") as fid:
a = np.array([[1, 2], [3, 4]])
fid.write(a.astype("float64"))
# a.astype("float64").tofile(fid) # also doesn't give correct result
Python result (as shown by notepad):
ð? @ @ @


a = [[1 2];[3 4]];has a lot of redundant brackets, it is better to writea = [1 2; 3 4];.