converting uint8 to double in a faster way

258 Ansichten (letzte 30 Tage)
HYZ
HYZ am 25 Okt. 2022
Kommentiert: Bruno Luong am 25 Okt. 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 Kommentar
DGM
DGM am 25 Okt. 2022
I imagine that allocating and filling ~3GB of contiguous memory could take some time depending on how much memory you have.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 25 Okt. 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 Kommentare
Walter Roberson
Walter Roberson am 25 Okt. 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 am 25 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by