How to save data in 32-bit binary in Matlab?

I need to check some "random numbers" with diehard generated by a program in MATLAB. Diehard only accepts a 32-bit binary (single precision), but if I save my data in MATLAB, it will be saved in the binary with double precision (so the binary is 2 * 64 = 128 bits). How to create a 32-bit binary in MATLAB running on a 64-bit system?

+3
source share
2 answers

If you want to read / write data to a binary file in a specific format, you must use the FREAD / FWRITE functions . For example, this will write 100 random values ​​to a file in the form of 32-bit floats:

A = rand(1,100);
fid = fopen('temp.dat','wb');
fwrite(fid,A,'float32');
fclose(fid);

For more information about the IO file in MATLAB, you can also check out these other related SO posts: here and here .

+6
source

In addition to the gnovice solution, you can create random numbers as "single", for example:

rand(1, 100, 'single')
+3
source

Source: https://habr.com/ru/post/1709707/


All Articles