【发布时间】:2019-01-06 02:37:23
【问题描述】:
我正在处理二进制信息,从文件中读取。这是一个 40 位格式的数字序列,在我的例子中,前 8 位应该被忽略,另外 32 位是“混洗”的 32 位单精度 IEEE 754 格式。这种“洗牌”非常简单:当我采用以下位顺序时,我得到了正确的 IEEE 754 binary32:24-32、17-24、9-16
所有这些都是用下面的代码模拟的。
问题:如何改进下面的代码以使其更快,摆脱“for”循环并使用高效的 MATLAB 矩阵运算?
a = (1:5*8*1000000)'; % represent indices of bits binary information, read from file
tic
a_reshaped = reshape(a, 8, [])';
toc %Elapsed time is 0.176375 seconds.
n_elem = size(a_reshaped,1)/5;
result = zeros(n_elem,8*4);
for i = 1:n_elem
result(i,:) = [a_reshaped(5*i,:) a_reshaped(5*i-1,:) a_reshaped(5*i-2,:) a_reshaped(5*i-3,:)];
end
toc %Elapsed time is 4.243868 seconds.
【问题讨论】:
-
该文件的字节序似乎与您的计算机不同。您可以在打开文件时更改字节顺序,因此您以后不需要重新排列字节。