【发布时间】:2025-12-04 06:25:01
【问题描述】:
我正在尝试加快位转换过程,该过程当前处于 MATLAB 循环中并且需要很长时间才能执行。问题来了:
考虑一个大小为12xN 的input 整数矩阵,其中N 可以很容易地为100,000 左右。然后我执行以下操作:
output = zeros(3, N);
temp = zeros(3,1);
% Loop through each column
for pp = 1 : N
% For each column, convert the first 4 numbers into a float, the second 4 numbers into a float, and finally the third 4 numbers into a float as so:
for dd = 1:3
snip = input(dd * 4 : -1 : (dd-1) * 4 + 1, pp);
temp(dd) = typecast(uint32(bin2dec(num2str(reshape(dec2bin(snip,8).',1, 32))) ), 'single');
end
% Now simply store the result
output(1:3,pp) = temp;
end
我基本上是将每列转换为 3 个浮点数,但我想看看是否有任何方法可以加快此过程,并删除您在此处看到的循环。
有没有这样的方法?谢谢!
编辑:
这里有一个例子来说明我的目标:假设我们的inputMatrix 是一个12 x 4,由:
65 65 66 65
164 168 175 174
130 232 2 222
16 138 86 27
64 64 66 65
209 240 59 12
136 185 207 101
103 33 18 172
190 190 64 190
185 182 121 184
36 41 153 173
19 55 127 183
转换后的输出(请参阅here 了解通过 IEEE-754 标准转换/解包到浮点),将是 output 的值:
20.5635 21.1135 87.5046 21.8584
6.5479 7.5226 46.9522 8.7748
-0.3616 -0.3558 3.9000 -0.3607
因此,inputMatrix 的第一个 12 元素列将给出 output 的第一个 3 元素列。 (换句话说,[16, 164, 30, 16] 将给出 20.5635,而 [16, 64, 209, 136] 将给出 6.5479 等)。
我想要做的是,它尽可能地将这个矩阵上的这个操作向量化。谢谢!
【问题讨论】:
标签: performance matlab type-conversion bit-manipulation vectorization