Vectorization of for loop (indexing of a vector)
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
MyZ Zhang
el 13 de Nov. de 2015
Editada: Joss Knight
el 18 de Nov. de 2015
Please help, I'm trying to get this code to run faster on my GPU. Currently this is the main bottleneck of my function. I was wondering whether this code is vectorizable in order to remove the for-loop? vector_1 is a gpuArray
for i=1:n
A = vector_1((i-1)+(1:10));
B = vector_1((i-1)+16+(1:10));
AA(i+10) = (A'*B)/10;
BB(i+10) = (A'*A)/10;
end
0 comentarios
Respuesta aceptada
Joss Knight
el 18 de Nov. de 2015
Editada: Joss Knight
el 18 de Nov. de 2015
first = gpuArray.colon(1,n);
indexA = bsxfun(@plus, first, [0:9]');
indexB = indexA + 16;
A = vector_1(indexA);
B = vector_1(indexB);
AA(first+10) = sum(A.*B)/10;
BB(first+10) = sum(A.*A)/10;
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!