How to speed up this code?

I'm trying to speed up a code made of several functions and, when I run the profiler, I see that the function that takes the most time is the following in this part.
n=size(Amat,2);
for k=1:size(Amat,1)
cv(k,1)= copula_Clayton(Amat(k,1:n),alpha_0);
end
Is it possible to eliminate that for loop and make the copula_Clayton func run on every row of the matrix Amat(k,1:n) automatically?

댓글 수: 1

José-Luis
José-Luis 2014년 7월 22일
Two things:
  1. Pre-allocate cv
  2. Re-write copula_Clayton to that it takes the whole Amat as input.
Please note that since we have no way of guessing at what copula_Clayton actually does, it is very difficult for us to suggest possible improvements.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 22일
편집: Azzi Abdelmalek 2014년 7월 22일

0 개 추천

You can use arrayfun, but you will notice that the for loop is faster.
cv=arrayfun(@(k) copula_Clayton(Amat(k,1:n),alpha_0),(1:size(Amat,1))','un',0);

댓글 수: 1

Giacomo
Giacomo 2014년 7월 22일
I've already tried and I noticed it was taking longer that the loop itself.

이 질문은 마감되었습니다.

질문:

2014년 7월 22일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by