VERY slow for loop

7 ビュー (過去 30 日間)
sani
sani 2020 年 1 月 18 日
編集済み: sani 2020 年 1 月 19 日
I have a very slow for loop in my script, it meant to sort a column in a text file (about 500,000 rows) and remove lines with negative values. it took ~10600 sec to finish this lines. Is this normal? any way to improve it?
this is the code:
for i = 1:height (T1(:,2)) %remove negative numbers from column 2
if lt(T1.energy,0)
continue;
else
T1(T1.energy < 0, :) = [];
end
end
thanks!

採用された回答

Walter Roberson
Walter Roberson 2020 年 1 月 18 日
You do not use the variable i inside your for loop, so you are testing the entire energy variable of the table every time through the loop.
if lt(T1.energy,0)
is a vector test, and will only be true if all of the entries in T1.energy are < 0 .
You probably just want,
T1(T1.energy < 0, :) = [];
with no loop -- just the single statement.

その他の回答 (1 件)

sani
sani 2020 年 1 月 19 日
編集済み: sani 2020 年 1 月 19 日
thanks! it reduced to 0.3 sec, amazing!

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by