How to vectorize for loop of partial arrays?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Geoff Hayes
el 22 de Nov. de 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.
Respuestas (1)
Lessmann
el 23 de Nov. de 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 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!