Most time-efficient conversion from logical matrix to fixed-point vector
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Matt J
le 6 Avr 2024
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Fixed Point dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!