필터 지우기
필터 지우기

Remove For Loops to Optimize Performance

조회 수: 2 (최근 30일)
Dinuk Amarasinghe
Dinuk Amarasinghe 2022년 10월 6일
답변: Taru 2022년 10월 7일
cvx_begin
y = 0;
n = 1000;
variables x;
minimize y;
parfor i = 1:n
y = -sum(log(1-x^2)) - sum(log(4 + rand(i)*x));
end
cvx_end
Need to remove for loops and write directly as vectors to speed up optimization algorithm.
  댓글 수: 1
Torsten
Torsten 2022년 10월 6일
rand(i) generates an (ixi) matrix of uniformly distributed random numbers on [0 1].
I doubt this is what you want.

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

답변 (1개)

Taru
Taru 2022년 10월 7일
Hi,
There seems to be a misunderstanding of the rand function as rand(i) gives a i*i square matrix of uniformly distributed random numbers between [0 1]. I understand that you might want it to generate n random numbers. So, you can just use rand(1,n) and vectorise it as:
y = -sum(log(1-x^2)) - sum(log(4 + rand(1,n)*x));
Without using any loop of sorts.

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by