필터 지우기
필터 지우기

How to speed up find and unique

조회 수: 4 (최근 30일)
Chaoyang Jiang
Chaoyang Jiang 2018년 4월 26일
댓글: Chaoyang Jiang 2018년 4월 26일
I did the profiling where unique and find take the most of time.
I use find as I want to know the row and column index of the 1 elements in a. For unique, I only need the index.
a=randi([0 1], 20000,8736);
temp=randi(8737, 4000,1000);
for i=1000
[aa0,aa00]=find(a(:,temp(:,i))==1);
[~,aa001,~]=unique(aa00);
end
May I know how to speed up? Thank you.
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 4월 26일
I think your aa001 should be equivalent to
aa001 = find(diff([0; aa0]));
which is an abbreviation for
aa001 = [1; find(diff(aa0) ~= 0)];
which is looking for the places where aa0 changes.
Potentially faster would be
aa001 = [1; find(aa0(1:end-1) ~= aa0(2:end))];
but with the two intermediate arrays you would really have to do a timing test to be sure which variation was faster.
Chaoyang Jiang
Chaoyang Jiang 2018년 4월 26일
Thank you for your answer. The output of aa001 = find(diff([0; aa0])) is the unique value, while in my code, [~,aa001,~]=unique(aa00) the output is the unqiue value index. They are not same.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by