필터 지우기
필터 지우기

How to vectorize for loop of partial arrays?

조회 수: 3 (최근 30일)
Ravi Goyal
Ravi Goyal 2015년 11월 22일
댓글: Ravi Goyal 2015년 11월 23일
I am tryig to accelerate this part of my code, as it consumes the most of the runtime. I posted a simplified snippet which has the core functionality. Is there any way to use arrayfun or bsxfun, as my attempts have failed so far. See code below. Thanks a bunch. Ravi
X=zeros(length(k),length(k));
for i=1:length(f)
X=X+(a(:,i)*a(:,i)')/b(i);
end
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2015년 11월 22일
Ravi - what are the dimensions of a? Is it just a two-dimensional array whereby you multiply each column by itself and then divide each column by ith element of b? But if that were true, then you wouldn't need to initialize X as a two dimensional array (it would just be a single column).
Please provide some details concerning your a, b and why X is initialized using the length of k but you iterate over the length of f.
Ravi Goyal
Ravi Goyal 2015년 11월 23일
Geoff, X becomes a square matrix, for instance 4x4. However, as the function gets called many times, the matrix grows and can be up 80x80. I multiply each column of a with its transposed and dived that by a single element of vector b, which yields me the quadratic matrix X. Each time the matrix X is calculated for all frequencies f. a has the size(k,f). k can also change as the function is recursive and grows up to f, mostly to terminate earlier though as an optimum is found later on in the function evaluation solving X*k=b for a vector k.

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

답변 (1개)

Lessmann
Lessmann 2015년 11월 23일
Hi,
this is a vectorized version of your loop.
k = ones(1000,1);
b = 10*rand(25,1);
f = ones(25,1);
a = 10*rand(1000,25);
c = 1./repmat(b',length(k),1);
X = (a.*c)*a';

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by