What is the fastest, circshift or indices ?

조회 수: 7 (최근 30일)
Stéphane
Stéphane 2014년 11월 5일
편집: Matt J 2014년 11월 5일
Hi
I'm trying to improve the efficiency of my algorithm. I need to compute something like :
Y(2:end-1,:) = X(2:end-1,:) + X(1:end-2,:) - X(3:end,:)
For Y and X n by m matrices. I was wondering if it was faster to do it as it is above, or using circshift :
Y = X + circshift(X,[-1,0]) - circshift(X,[+1,0]).
I've done a few tests but I can't get a clear idea... The aim is to do this on pretty big matrices on a cluser with multiple cores...
Any idea ? Thanks.
Stéphane
  댓글 수: 1
Adam
Adam 2014년 11월 5일
I regularly create small test scripts with as many alternative implementations as I can think of, each wrapped in a
timeit(f)
instruction for function handle, f. It is extremely useful for improving understanding as well as getting the (hopefully conclusive) final answer as to which method is fastest. Sounds like you did kind of do that though...

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

채택된 답변

Matt J
Matt J 2014년 11월 5일
편집: Matt J 2014년 11월 5일
Just use conv2
Y=conv2(X,[-1;1;1],'valid');
As for circshift, it is not a builtin function and, if you look inside it
>>type circshift
you will see that it does pretty much the same indexing as you are doing. So, I wouldn't expect it to be a competitive approach.
  댓글 수: 4
Stéphane
Stéphane 2014년 11월 5일
Second thought...
I've tried in 1D to understand what conv (conv2) does and it seems conv(X,[-1;1]) and the same with circshift doesn't produce the same result. Is it normal or am I doing something wrong ?
(What I want is X(2:end) - X(1:end-1) )
Matt J
Matt J 2014년 11월 5일
편집: Matt J 2014년 11월 5일
(What I want is X(2:end) - X(1:end-1) )
That would correspond to
conv(X,[1;-1],'valid')
You could also do
diff(X)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by