필터 지우기
필터 지우기

How can I avoid using for loop in this functions

조회 수: 1 (최근 30일)
祥宇 崔
祥宇 崔 2023년 3월 26일
댓글: 祥宇 崔 2023년 4월 11일
This is the function whose speed I want to improve. I know I should use more matrix operation and use less for loop. But I have no idea how to use if-else in matrices.
function y=Besselj_approx(n,z)
y=zeros(1,length(n));
for i=n
if i>0
y(i)=1./gamma(i+1).*(z/2).^i;
elseif i<0
y(i)=(-1).^(-i)./gamma(-i+1).*(z/2).^(-i);
else
y(i)=-z.^2/4+1;
end
end
Any help is appreciated!
  댓글 수: 4
祥宇 崔
祥宇 崔 2023년 3월 26일
@Dyuman Joshi Thanks! Guess the for loop is alright.
祥宇 崔
祥宇 崔 2023년 3월 26일
@Rik Thanks! I will give it a try.

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

채택된 답변

KSSV
KSSV 2023년 3월 26일
function y=Besselj_approx(n,z)
i = n ;
y=zeros(1,length(n));
y(1:end)=-z.^2/4+1;
y(i>0) = 1./gamma(i(i>0)+1).*(z/2).^i(i>0);
y(i<0)=(-1).^(-i(i<0))./gamma(-i(i<0)+1).*(z/2).^(-i(i<0));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by