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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by