Borrar filtros
Borrar filtros

converting uint8 to double in a faster way

149 visualizaciones (últimos 30 días)
HYZ
HYZ el 25 de Oct. de 2022
Comentada: Bruno Luong el 25 de Oct. de 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 comentario
DGM
DGM el 25 de Oct. de 2022
I imagine that allocating and filling ~3GB of contiguous memory could take some time depending on how much memory you have.

Iniciar sesión para comentar.

Respuesta aceptada

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

Más respuestas (1)

Walter Roberson
Walter Roberson el 25 de Oct. de 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 comentarios
Walter Roberson
Walter Roberson el 25 de Oct. de 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 el 25 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by