Vectorize strcat and str2num for loop

조회 수: 3 (최근 30일)
Jed Abanat
Jed Abanat 2019년 3월 27일
댓글: madhan ravi 2019년 3월 27일
I'm trying to speed up this for loop function. Basically want to to concatonate two integrers, but as the columns and frames variables contain different number of significant figures, I need to include leading zeros. I have tried using a decimal place to merge the values, but the rounding fails for large column/frame numbers. Is there any way I can speed up this for loop, or other efficient ways to do this?
nframes = 1800;
ncols = 801;
columns = 1:ncols;
frames = 1:nframes;
frames = repmat(frames,ncols,1);
columns = repmat(columns,1,nframes);
frames = frames(:);
columns = columns(:);
ID = zeros(1,nframes*ncols);
tic
for xx = 1:nframes*ncols
ID(xx) = str2num(strcat(sprintf('%03i',columns(xx)),sprintf('%04i',frames(xx))));
end
toc
Thanks.
  댓글 수: 2
Jed Abanat
Jed Abanat 2019년 3월 27일
That works great thank you!
madhan ravi
madhan ravi 2019년 3월 27일
[comment moved to answer section]

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

채택된 답변

madhan ravi
madhan ravi 2019년 3월 27일
See if this is faster than a loop:
columns * 1e4 + frames

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by