フィルターのクリア

computational time nested if commands

1 回表示 (過去 30 日間)
Stelina
Stelina 2014 年 9 月 9 日
編集済み: Andrei Bobrov 2014 年 9 月 10 日
Hi there,
I am attaching an mfile with nested ifs - it is really simple and I include the commenting, so it is easily read.
However, it takes for ever to execute. The weird thing is that if I exclude only one condition (one nested if - the >1.11 Z see code) , it executes really fast. I really cannot spot weather there is a mistake in the code - it doens't return anth as an error, just simply never stops, so I was just hoping for you MATLAB guru's.
Thanx a lot in advance! Stella

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 9 月 9 日
編集済み: Andrei Bobrov 2014 年 9 月 10 日
us1 = abs(Us(:,1:48));
x = us1(:,[1:30,36:48]);
z = us1(:,[31,32,34,35]);
p = us1(:,33);
l1 = all([bsxfun(@gt,p,2*x),2), bsxfun(@gt,p,1.11*z)],2);
Usvalid = Us(l1,:);
Usnonvalid = Us(~l1,:);
  2 件のコメント
Stelina
Stelina 2014 年 9 月 9 日
Andrei thanx for the answer. If I understand correctly, the code is equivalent with (i was getting error):
us1 = abs(Us(:,1:48));
x = us1(:,[1:30,36:48]);
z = us1(:,[31,32,35,36]);
peak = us1(:,33);
criterion1=bsxfun(@gt,peak,2*x)
criterion2=bsxfun(@gt,peak,1.11*z)
l1 = all([criterion1, criterion2],2);
Usvalid = Us(l1,:);
I get correct results. However, since criterion1 and criterion2 are my new arrays as returned by bsxfun, how does the all command above work?
And, still, do you perhaps have any idea what was going wrong with the first code with the nested ifs?
Thanx, Stella
Andrei Bobrov
Andrei Bobrov 2014 年 9 月 10 日
編集済み: Andrei Bobrov 2014 年 9 月 10 日
variant with loop for..end and condition if..else..end:
us1 = abs(Us(:,1:48));
x = us1(:,[1:30,36:48]);
z = us1(:,[31,32,35,36]);
peak = us1(:,33);
k = 0;
m = 0;
for ii = 1:size(Us,1)
if all(peak(ii) > 2*x(ii,:)) && all(peak(ii) > 1.11*z(ii,:))
k = k + 1;
Usvalid(k,:) = Us(ii,:);
else
m = m + 1;
Usnonvalid(m,:) = Us(ii,:);
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by