Effacer les filtres
Effacer les filtres

converting uint8 to double in a faster way

149 vues (au cours des 30 derniers jours)
HYZ
HYZ le 25 Oct 2022
Commenté : Bruno Luong le 25 Oct 2022
Hi,
I would like to convert to uint8 array to double. I attached the struct temp.mat here which contains unit8 for 60 frames. it took like 16s for converting all frames into double.
Could you suggest if there is faster way to convert to double?
Thanks in advance.
load('temp.mat')
dotspos = zeros (1080, 1920, 3, nFrames);
for i = 1:nFrames
temp1 (:,:,:,i) = double(temp(i).cdata);
dotspos = temp1;
end
  1 commentaire
DGM
DGM le 25 Oct 2022
I imagine that allocating and filling ~3GB of contiguous memory could take some time depending on how much memory you have.

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 25 Oct 2022
dotspos = double(cat(4,temp.cdata));

Plus de réponses (1)

Walter Roberson
Walter Roberson le 25 Oct 2022
load('temp.mat')
dotspos = zeros (1080, 1920, 3, nFrames);
for i = 1:nFrames
dotspos(:,:,:,i) = double(temp(i).cdata);
end
You did not preallocate temp1 so you were growing it every iteration.
  3 commentaires
Walter Roberson
Walter Roberson le 25 Oct 2022
Note:
converting one frame at a time requires storage equal to what you zeros() plus temporary storage the size of one frame converted to double precision.
Using double(cat(4)) is a nice compact method, and would be quite reasonable to see in code. However, it requires temporary storage equal to the total size of the frames in uint8. I would expect that the cat(4) method is faster if you have enough RAM (slower if the extra space requires that you write to swap space.)
Bruno Luong
Bruno Luong le 25 Oct 2022
If memory space is the concern one can keep 1/2 variables at the same time not three
%function temp = cast2dbl(temp)
temp = cat(4,temp.cdata);
temp = double(temp);
%end
Just a detail, but might be important if RAM is limiting. At worst the intermediate RAM is requires 1/8 extra than the space to store the final result.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion 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!

Translated by