How to speed up normalization of an array

조회 수: 3 (최근 30일)
Arti Siddhpura
Arti Siddhpura 2014년 7월 2일
댓글: Arti Siddhpura 2014년 7월 2일
I have an array say selfn of size 1000X3. I need to normalize it and here is the code I am using:
selfn = selfn ./ repmat( sqrt( sum( selfn.^2, 2 ) ), 1, 3 );
Now this is a bottleneck in my code. Itried using bsxfun as:
mynews=ones(size (selfn)); selfn=bsxfun(@rdivide,selfn,bsxfun(@times,mynews,bsxfun(@power,sum(bsxfun(@power,selfn,2),2),0.5)));
But this doesn't make much change to the time it takes. I was wondering if I can use hypot to speed up this part. Any thoughts or suggestions on further speed improvement? I would really appreciate it if anyone can help.

답변 (1개)

W. Owen Brimijoin
W. Owen Brimijoin 2014년 7월 2일
You don't have to use bsxfun three times in that line, just the once. Try this simpler line:
selfn = bsxfun(@rdivide,selfn,sqrt(sum(selfn.^2,2)));
That is ever so slightly faster than your repmat solution (on my machine the elapsed time is 0.001946 versus 0.002416 seconds), but I fear you aren't going to get a whole lot quicker than that. Anyone else know how to beat that time?
  댓글 수: 1
Arti Siddhpura
Arti Siddhpura 2014년 7월 2일
Thanks W. Owen Brimijoin. And yeah you are right, it doesn't change elapsed time much.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by