converting uint8 to double in a faster way

조회 수: 174 (최근 30일)
HYZ
HYZ 2022년 10월 25일
댓글: Bruno Luong 2022년 10월 25일
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
DGM
DGM 2022년 10월 25일
I imagine that allocating and filling ~3GB of contiguous memory could take some time depending on how much memory you have.

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
Bruno Luong 2022년 10월 25일
dotspos = double(cat(4,temp.cdata));

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 10월 25일
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
Walter Roberson
Walter Roberson 2022년 10월 25일
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 2022년 10월 25일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by