Borrar filtros
Borrar filtros

Issue with making an array

1 visualización (últimos 30 días)
Tony
Tony el 23 de Abr. de 2016
Comentada: Tony el 24 de Abr. de 2016
Here is my code:
function Tfunc(a,b)
a = (-(a-1)/a);
b = (-(b-1)/b);
for i = 1:30000
Approximation_b(i) = ((-1)^(1+i)) * ((b^i)/i);
Approximation_a(i) = ((-1)^(1+i)) * ((a^i)/i);
end
sum(Approximation_b)/sum(Approximation_a)
end
I tried making the following change (2 lines under function):
function Tfunc(a,b)
Approximation_b = zeros(30000);
Approximation_a = zeros(30000);
a = (-(a-1)/a);
b = (-(b-1)/b);
for i = 1:30000
Approximation_b(i) = ((-1)^(1+i)) * ((b^i)/i);
Approximation_a(i) = ((-1)^(1+i)) * ((a^i)/i);
end
sum(Approximation_b)/sum(Approximation_a)
end
I thought adding this would help the code run faster since I am creating the array ahead of time, but all I get is a slower code that 1) doesn't run, 2) memory problem.
Why am I getting a memory problem if the first code works just fine?

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Abr. de 2016
Approximation_b = zeros(1, 30000);
You were creating a 30000 by 30000 array.

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays 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!

Translated by