How to improve the speed of of this function?

2 ビュー (過去 30 日間)
Chameleon17
Chameleon17 2016 年 2 月 15 日
編集済み: Stephen23 2016 年 2 月 16 日
Good Morning, I am looking for some advice regarding improving the efficiency of my code. I have a simple loop I am running but it is the first time I am running it on the entirety of my data set and I now realize how slow my loop is. I am wondering if anyone might have some advice for improving the speed.
TRIAL1A_SPACE1 = zeros(size(T1A_raw_TEMP))
[r, c] = size(TRIAL1A_SPACE1)
for e = 1:r
for f = 1:c
if T1A_raw_TEMP(e,f) >= 8 && T1A_raw_HUM(e,f) >= 11
TRIAL1A_SPACE1(e,f) = 1
end
end
end
The [r,c] = [7824,183].
Thank you for any help or advice!

回答 (2 件)

Ingrid
Ingrid 2016 年 2 月 15 日
you do not need a loop for this, you can just use
TRIAL1A_SPACE1 = zeros(size(T1A_raw_TEMP));
TRIAL1A_SPACE1(T1A_raw_TEMP >= 8 && T1A_raw_HUM >= 11 ) = 1;
  1 件のコメント
Chameleon17
Chameleon17 2016 年 2 月 15 日
Hi, thanks very much for your quick response.
I have tried using it without a loop but I get the error Operands to the and && operators must be convertible to logical scalar values.
I found another thread http://uk.mathworks.com/matlabcentral/newsreader/view_thread/169749 which seems to suggest the only way around this is a loop?

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


Jos (10584)
Jos (10584) 2016 年 2 月 15 日
The command
TRIAL1A_SPACE1 = T1A_raw_TEMP >= 8 & T1A_raw_HUM >= 11
will make create the required (logical) matrix.
  2 件のコメント
Chameleon17
Chameleon17 2016 年 2 月 15 日
Thank you very much for that! It worked!
I have another follow up question though, trying to get around loops that will take a very long time to execute.
TRIAL1A_SPACE3 = zeros(size(TRIAL1A_SPACE1)) [s, d] = size(TRIAL1A_SPACE3)
for g = 1:s for h = 1:d-1 if TRIAL1A_SPACE1(g,h) == 1 && TRIAL1A_SPACE1(g, h+1) == 1 TRIAL1A_SPACE3(g,h+1) = 1 end end end
is there a simpler way to complete the above, without looping, but I want to score for when an even has occurred for two consecutive days.
Jos (10584)
Jos (10584) 2016 年 2 月 16 日
g = 1:s
h = 1:d-1
TRIAL1A_SPACE3(g,h+1) = TRIAL1A_SPACE1(g,h) == 1 & TRIAL1A_SPACE1(g, h+1) == 1

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

カテゴリ

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