How to simplify a code to remove a for loop and make it faster?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I'm still learning vectorization and haven't figured out how to simplify this code:
B=zeros(1,a^2);
for j=1:a^2;
B(:)=B(:)+C(:,j)*D(j);
end;
in which B is a vector of 1 row by a^2 columns, C is an a x a matrix, and D is a vector of 1 row by a^2 columns
So basically what is happening is I take all the columns of C (one by one) and multiply them by D and then add them to B but this is repeated column by column on C.
I appreciate if anyone can show me how to do this.
Thank you very much.
0 comentarios
Respuestas (3)
Iain
el 14 de Ag. de 2014
One of these ought to do it:
B = C' * D;
or
B = C' * D';
You might need to transpose B to get what you want....
0 comentarios
Azzi Abdelmalek
el 14 de Ag. de 2014
Editada: Azzi Abdelmalek
el 14 de Ag. de 2014
Edit
a=4
C=rand(a^2);
D=rand(1,a^2);
B=sum(bsxfun(@times, C,D),2)
2 comentarios
Azzi Abdelmalek
el 14 de Ag. de 2014
Editada: Azzi Abdelmalek
el 14 de Ag. de 2014
Look at edited answer
Ver también
Categorías
Más información sobre Logical 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!