How to avoid the two loops with a less memory consumption.

Hello everyone,
I have used two for loops for one problem. Can anyone suggest me a way to avoid the loops and reduce the time. is it possible to calculate A without using a for loop ? I am using R2015a.
for i=1:100000
for j=1:100000
A(i,j)=exp(-(x(i)-x(j))-(y(i)-y(j)));
end
end

 Respuesta aceptada

Stephen23
Stephen23 el 7 de Sept. de 2020
Editada: Stephen23 el 7 de Sept. de 2020
A = exp(x(:).'-x(:) + y(:).'-y(:));
This requires MATLAB version >=R2016b. For earlier versions replace the subtraction with bsxfun:
A = exp(bsxfun(@minus,x(:).',x(:)) + bsxfun(@minus,y(:).',y(:)));
Note: avoiding loops will not have "less memory consumption", most likely more.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de Sept. de 2020

Editada:

el 7 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by