How is modeled the more short time this loop?

1 visualización (últimos 30 días)
Erkan
Erkan el 28 de Ag. de 2021
Comentada: Erkan el 28 de Ag. de 2021
Hi everybody, i have the for loop, but loop time is very long. How can i make the more short time?
R=rand(1,3000);
W=zeros(1,length(R));
for i=1:length(R)+1
for ii=1:length(R)-i+1
W(i)=W(i)+R(ii)*R(ii+i-1)
end
end

Respuesta aceptada

DGM
DGM el 28 de Ag. de 2021
Editada: DGM el 28 de Ag. de 2021
I bet this can be simplified further, but here.
l = 4;
R = rand(1,l);
W = zeros(1,l);
for k = 1:l
W(k) = sum(R(1:l-k+1) .* R(k:end));
end
EDIT: Updated to match question change. Also, echoing to console takes a considerable amount of time.
  4 comentarios
DGM
DGM el 28 de Ag. de 2021
Edited.
Erkan
Erkan el 28 de Ag. de 2021
Yes, it is right working, thank you so much.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by