Saving data as binary
조회 수: 14 (최근 30일)
이전 댓글 표시
Basically, i have for example k = [0 5 4], i want it to be saved as [0 101 100] instead of [00000000 00000101 00000100] so that it takes the least size possible, how can i do that ?
댓글 수: 0
답변 (2개)
Voss
2022년 5월 14일
k = [0 5 4];
arrayfun(@(x)dec2bin(x,max(1,ceil(log2(x)))),k,'UniformOutput',false)
댓글 수: 11
Walter Roberson
2022년 5월 20일
bits = {[1] [0 0] [1] [0 1 1] }
Bitstream = [bits{:}];
fid = fopen('test.bin','w');
fwrite(fid, Bitstream, 'bit1');
fclose(fid);
Ilya Dikariev
2022년 5월 20일
k_new=str2num(dec2bin(k))' would do. But if you want to still reduce the the size, just use dec2bin which keeps the data in char type which is 8 times smaller
댓글 수: 1
Walter Roberson
2022년 5월 20일
편집: Walter Roberson
2022년 5월 20일
only 4 times smaller. Each character needs 16 bits.
If you uint8(k_new) then that would need only one byte per value
참고 항목
카테고리
Help Center 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!