Most time-efficient conversion from logical matrix to fixed-point vector
1 次查看(过去 30 天)
显示 更早的评论
I have a n-by-m logical matrix A and I need to interpret it as a n-by-1 fi-object vector b, so that each row of A is the binary representation of the corresponding element of b, i.e., bin(b(p)) is equal to char(A(p, :) + 48) for each row p.
The most time-efficient way that I can think to is to use an intermediate conversion to char and assign it to the binary representation of the fi-object vector, as the following example program shows.
n = 1e6; % number of fi objects
m = 16; % number of bits of each fi object
tic;
A = randi(1, [n m], "logical");
t1 = toc(); % measure how long it takes the generation of random data
b = fi(zeros(n,1), false, m, 0);
tic; % measure how long it takes my conversion strategy
b.bin(:) = char(A + 48); % "slow"...how to do it faster?
t2 = toc();
assert(isequal(bin(b), char(A + 48))) % check that the result is what I meant
As a comparison, on an Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz, t1 is about 0.13 seconds whereas t2 is 46.3 seconds, that is, my strategy is about 356x slower than the random number generation.
Am I missing some more time-efficient way to do this?
0 个评论
采纳的回答
Matt J
2024-4-6
n = 1e6; % number of fi objects
m = 16; % number of bits of each fi object
tic;
A = randi([0,1], [n m], "logical");
t1 = toc() % measure how long it takes the generation of random data
tic; % measure how long it takes my conversion strategy
b = fi(A*2.^(m-1:-1:0)', false, m, 0);
t2 = toc()
assert(isequal(bin(b), char(A + 48))) % check that the result is what I meant
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fixed Point 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!