How can I replace the for loops?
L=(length(time)-1);
for q=1:numPT
for h=1:L
x(q,h)=SortDate{q+numPT*(h-1),6};
y(q,h)=SortDate{q+numPT*(h-1),7};
c(q,h)=SortDate{q+numPT*(h-1),5};
end
end

4 件のコメント

pg
pg 2017 年 8 月 9 日
As I have to process a large amount of data, so I need to find ways to replace the for loops. Thanks
Image Analyst
Image Analyst 2017 年 8 月 9 日
What does "large" mean to you? Like 1000, or 100 million? What are typical values for L and numPT?
pg
pg 2017 年 8 月 9 日
around 10 million, so it's better with I get replace the for loops.
Stephen23
Stephen23 2017 年 8 月 9 日
編集済み: Stephen23 2017 年 8 月 9 日
"so it's better with I get replace the for loops"
Why? What is the problem with them?
Loops are not slow. Badly written code is slow, but as you have not shown us much of your code then we don't have enough information to know what changes your code might need, if any. For example, are the output arrays preallocated?. The most useful information for you is contained on this page, and the pages it links to:

サインインしてコメントする。

 採用された回答

Jan
Jan 2017 年 8 月 9 日
編集済み: Jan 2017 年 8 月 9 日

1 投票

Before you start to vectorize the code, did you pre-allocate the output before the loops? This is essential:
L = (length(time)-1);
x = zeros(numPT, L);
y = zeros(numPT, L);
c = zeros(numPT, L);
for q = 1:numPT
for h=1:L
x(q,h) = SortDate{q+numPT*(h-1),6};
y(q,h) = SortDate{q+numPT*(h-1),7};
c(q,h) = SortDate{q+numPT*(h-1),5};
end
end
Now compare the timings. Then in the next step remove the inner loop:
L = (length(time)-1);
x = zeros(numPT, L);
y = zeros(numPT, L);
c = zeros(numPT, L);
for q = 1:numPT
x(q, :) = [SortDate{q+numPT*(0:L-1), 6}]; % Faster than: x(q, 1:L) = ...
y(q, :) = [SortDate{q+numPT*(0:L-1), 7}];
c(q, :) = [SortDate{q+numPT*(0:L-1), 5}];
end
And now the outer loop - and then a pre-allocation is not required:
L = (length(time)-1);
index = (1:numPT).' + (0:numPT:numPT*(L-1))); % >= R2016b
x = reshape(cell2mat(SortDate(index, 6)), size(index));
y = reshape(cell2mat(SortDate(index, 7)), size(index));
c = reshape(cell2mat(SortDate(index, 5)), size(index));
Do you see how the indices are moved from the for loop into the vector indices?
If you have an older Matlab version, you need bsxfun instead of the modern auto-expanding:
index = bsxfun(@plus, (1:numPT).', (0:numPT:numPT*(L-1)));
If this is still the bottleneck of you code, try to use the faster FEX: Cell2Vec, which seems to have a better memory management. Perhaps it is faster to convert the complete SortDate array at once:
L = (length(time)-1);
index = (1:numPT).' + (0:numPT:numPT*(L-1))); % >= R2016b
siz = size(index);
M = Cell2Vec(SortDate(:, 5:7));
x = reshape(M(index, 2), siz);
y = reshape(M(index, 3), siz);
z = reshape(M(index, 1), siz);

1 件のコメント

pg
pg 2017 年 8 月 10 日
Thanks so much. This is absolutely useful.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

pg
2017 年 8 月 9 日

コメント済み:

pg
2017 年 8 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by