converting uint8 to double in a faster way

175 views (last 30 days)
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 Comment
DGM
DGM on 25 Oct 2022
I imagine that allocating and filling ~3GB of contiguous memory could take some time depending on how much memory you have.

Sign in to comment.

Accepted Answer

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

More Answers (1)

Walter Roberson
Walter Roberson on 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 Comments
Walter Roberson
Walter Roberson on 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 on 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.

Sign in to comment.

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!