我正在使用从文件读取的二进制信息。这是一个数字序列,采用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.
答案 0 :(得分:1)
尝试一下:
ind = size(a_reshaped,1):-1:1;
ind(end:-5:1) = []; %remove the indices for rows you don't need
a_reduced = a_reshaped(ind,:);
result = flipud(reshape(a_reduced',8*4,[])');
答案 1 :(得分:0)
“ for”循环可以在以下一行代码中替换,这要快得多:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer