How do I create a faster ordered vector without for loop

6 ビュー (過去 30 日間)
Oluwaseun Lijoka
Oluwaseun Lijoka 2015 年 11 月 25 日
編集済み: Stephen23 2015 年 11 月 25 日
I discover that this pseudo code within my code makes my program slow probably because of the for loop and the fact that JJ is not pre-allocated. I will appreciate a faster idea.
JJ=[];
for j=0:p
JJ=[JJ; repmat(j,[1+((j+1)-1)*2,1])];
end

採用された回答

Stephen23
Stephen23 2015 年 11 月 25 日
編集済み: Stephen23 2015 年 11 月 25 日
X = floor(sqrt(0:(p+1)^2-1))';
This is nearly fifty times faster (p=1000):
Elapsed time is 5.2233 seconds.
Elapsed time is 0.111006 seconds.
As p gets larger it will reach your memory limit very quickly: each magnitude increase in p causes a two-magnitude increase in the number of elements of the output vector.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by