Find overlapped range between vectors

3 ビュー (過去 30 日間)
SSOI SS
SSOI SS 2014 年 5 月 9 日
コメント済み: SSOI SS 2014 年 5 月 9 日
I have a simple perhaps math question:
There are 5 vectors despiting their ranges, e.g.
[ 5 10]
[ 7 20]
[12 26]
[ 6 9]
[ 0 3]
How can I find their overlapped range? (Desired result is [7 9])
I came to a solution with two for loops, but it will be very time-consuming if the number of vectors is vast.
Does anyone have a simple and fast way to do this?
  2 件のコメント
Image Analyst
Image Analyst 2014 年 5 月 9 日
編集済み: Image Analyst 2014 年 5 月 9 日
What is the number of vectors? You can do tens of millions of for loop iterations in about a second so I doubt it's the for loop itself that is causing the delay. Maybe it's min() or max() being called so many times but I doubt it the iterating. Look, I did a billion iterations in just over 2 seconds :
tic
for k = 1 : 1e9
;
end
toc
Elapsed time is 2.269138 seconds.
SSOI SS
SSOI SS 2014 年 5 月 9 日
ok, I have to admit that the cases my algorithm have to deal with are more complicated, since the vectors can satuate and have overturns. So I have to keep correcting the elements. In total I get a result of 4.37 sec with 227230 loops. That's the moment I started to think alternatives for the for loops.
Maybe there is a better way ...

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

回答 (1 件)

Image Analyst
Image Analyst 2014 年 5 月 9 日
I think you need a for loop. Even if you were to somehow get all of your 227230 vectors into a 2D array that you then did some non-loop/vectorized method, you'd still have to use a loop to stuff the vectors into the 2D array in the first place , like say, you were reading the individual vectors from a text file, or Excel workbook, or retrieving them from a function or something.
  1 件のコメント
SSOI SS
SSOI SS 2014 年 5 月 9 日
I don't have create/stuff an extra 2D since the vectors are already in array format ...

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

カテゴリ

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