How to vectorize for loop of partial arrays?
1 view (last 30 days)
Show older comments
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 Comments
Geoff Hayes
on 22 Nov 2015
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.
Answers (1)
Lessmann
on 23 Nov 2015
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';
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!