Faster or smarter way to sort and intersect large amount of data??
조회 수: 1 (최근 30일)
이전 댓글 표시
Dear Sir/Madam,
I was using "intersect" in my Matlab code to do the sorting where I want the following:
[ch] = sort(s, 'ascend');
[same, a] = intersect(s, ch);
For example:
input: s =[55 21 78 7]
output: ch = [7 21 55 78] a = [4 2 4 3]
I need to access ‘a’ where ‘a’ shows the original index prior to sorting so I can use it for further processing.
This method works exactly as what I want, but I guess it is taking a lot of time to do the sorting and intersect etc especially when the size of s approaching 100 or higher, are there other faster or smarter ways to do so?
Thank you very much.
댓글 수: 2
José-Luis
2014년 2월 17일
What's a lot of time? Remember that the algorithmic complexity of sort is O(n log n) so there are limits to how fast you can make it, and Matlab is already pretty decent for this function.
채택된 답변
Jos (10584)
2014년 2월 17일
intersect is pretty fast, so I would not worry too much ...
a = rand(100000,1) ;
b = rand(100000,1) ;
tic ; [c,i] = intersect(a,b) ; toc ;
% Elapsed time is 0.029850 seconds.
댓글 수: 4
Jos (10584)
2014년 2월 17일
Yep. The profiler is a very useful tool if you want to know where improvements might be found.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!