How to vectorize For loop and array with indexing

조회 수: 1 (최근 30일)
shahrizan jamaludin
shahrizan jamaludin 2016년 3월 8일
댓글: shahrizan jamaludin 2016년 3월 11일
Hello, i want to vectorize the linecirc function below:
xout = zeros(nAngles,2); %360x2 size
yout = zeros(nAngles,2); %360x2 size
for i = 1:nAngles
[xout(i,:),yout(i,:)] = linecirc(slope(i),intercept(i),xp,yp,rp); %slope=intercept=1x360 size
end
before this, i tried to use arrayfun. unfortunately arrayfun is not faster than for loop. maybe indexing can make it faster. Thanks
  댓글 수: 2
Star Strider
Star Strider 2016년 3월 8일
What’s linecirc?
shahrizan jamaludin
shahrizan jamaludin 2016년 3월 8일
linecirc is a matlab function which to find intersections of circles and lines in Cartesian plane

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

답변 (1개)

Matthew Eicholtz
Matthew Eicholtz 2016년 3월 8일
Disclaimer: I do not have R2016a, so I cannot test the linecirc function.
But, if you already tried arrayfun and it was slower, I think the for-loop is going to be your fastest bet. Is there a reason (other than speed) that you want to vectorize the code? Sometimes a for-loop is the way to go.
  댓글 수: 3
Matthew Eicholtz
Matthew Eicholtz 2016년 3월 10일
Ah, I thought linecirc was exclusive to R2016a, but I see now that it is in earlier versions. However, it requires the Mapping Toolbox, which I do not have, so I still cannot test it.
Vectorization is not always the best route if you do not need it. Hence my question about your motives.
shahrizan jamaludin
shahrizan jamaludin 2016년 3월 11일
I managed to vectorize this code with arrayfun. but the time differen is not that much

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by