Loop erasing random walk
14 ビュー (過去 30 日間)
古いコメントを表示
Hi, I'm trying to find a fast way to detect loops in a vector. By loop I mean repetition of an integer and by loop erasing remove all integers between two occurrence of any integer For instance,
v=[3 1 4 6 7 9 1 22 87 33 35 36 37 35 34] should be equal to [3 1 22 87 33 35 34]
basically these are the nodes that I'm visiting in a graph and there may be loops. I just want to obtain a simple path (without loops). I found solutions but require "for" loops and I need this to be performant since it will be applied to every walk (100'000 walk of length up to 2'000 ). If anyone could help it will be greatly appreciated.
4 件のコメント
Jan
2011 年 5 月 25 日
In the example in the question, the both 1's are deleted. Here you keep the first (or last) value of the loop. Which behaviour is wanted?
採用された回答
Andrei Bobrov
2011 年 5 月 25 日
EDIT
V = v(:);
[a b n] = unique(V,'first');
[~, bl, nl] = unique(V,'last');
v1 = [b bl];
v1(abs(diff(v1,[],2)) < 100*eps,:) = [];
vv = min(v1(:)):max(v1(:));
idx = arrayfun(@(x)vv(vv>=v1(x,1) & vv<v1(x,2)),1:size(v1,1),'un',0);
V([idx{:}]) = []
removed typo
7 件のコメント
Jan
2011 年 5 月 25 日
UNIQUE calls SORT implicitely. If you inline the UNIQUE function, one of ther sortings could be omitted. But the complexity of the function would grow again.
その他の回答 (1 件)
Jan
2011 年 5 月 25 日
A FOR-loop approach, which might be fast already due to Matlab's JIT acceleration:
v = [3 1 4 6 7 9 1 22 87 33 35 36 37 35 34];
off = false; % Faster to call function FALSE once
n = length(v);
use = true(1, n);
for i = 1:n
if use(i)
multi = find(v((i + 1):n) == v(i), 1, 'last');
if isempty(multi) == 0
use((i + 1):(i + multi)) = off;
end
end
end
v = v(use);
If you expect long loops, a WHILE method can be faster:
off = false; % Faster to call function FALSE once
n = length(v);
use = true(1, n);
i = 1;
while i <= n
multi = find(v((i + 1):n) == v(i), 1, 'last');
if isempty(multi)
i = i + 1;
else
use((i + 1):(i + multi)) = off;
i = i + multi + 1;
end
end
v = v(use);
Using the smallest possible integer type for v will reduce the processing time:
v = int16(v);
5 件のコメント
Jan
2011 年 5 月 25 日
I get the same [98 17 21 57 1 49 99 54 62 76 85 68 66] with both versions. And 80 appears inside [49 80 49], such that it should *not* be there. Please check this again.
For your small test-vector of length 169 I get these timings (Matlab 2009a, 1.5GHz PentiumM):
tic; for i=1:20000, r = Andrei(v); end, toc % 4.39 sec
tic; for i=1:20000, r = JanFor(v); end, toc % 1.50 sec
tic; for i=1:20000, r = JanWhile(v); end, toc % 0.85 sec
I've constructed a longer testvector by [v,v+500,v+1000,...]. For a vector of length 13520 the WHILE method is 5 times faster than the 2*UNIQUE method.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!