why setdiff have time longer than for ?

5 ビュー (過去 30 日間)
huda nawaf
huda nawaf 2013 年 5 月 12 日
*hi,
Itried to make my code be faster , so i used setdiff instead of a part in my code
I used:*
v=(1:18);
buf=setdiff(v,unique_x);
relevant_item(ttt(i),1:length(buf))=buf;
*instead of *
for v=1:18
b2=find(unique_x==v);
if isempty(b2)
relevant_item(ttt(i),h)=v;
h=h+1;
end
end
*why the running time of this part is less than of setdiff?
thanks*

採用された回答

Walter Roberson
Walter Roberson 2013 年 5 月 12 日
setdiff() essentially has to sort each time -- or at least to check if the set is ordered.
Are the unique_x restricted to being in the allowed range for v, 1:18 ? If so, then how about
buf = 1:10;
buf(unique_x) = [];
relevant_item(ttt(i),1:length(buf))=buf;
  3 件のコメント
huda nawaf
huda nawaf 2013 年 5 月 15 日
Walter, if buf =[ 3 49 10 4 7 20];
I mean not 1:18, can find way to run the above code neither use setdiff , nor for?
thanks
Jan
Jan 2013 年 5 月 15 日
@huda: Look into the code of setdiff. You find the relevant part in the fast MEX function ismembc2, which expects pre-sorted data. So sort your data explicitly, call this function and use the replied indices. Note that ismembc2 is not documented, but you find enough information about it in the net.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by