Effacer les filtres
Effacer les filtres

Issue with making an array

1 vue (au cours des 30 derniers jours)
Tony
Tony le 23 Avr 2016
Commenté : Tony le 24 Avr 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?

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Avr 2016
Approximation_b = zeros(1, 30000);
You were creating a 30000 by 30000 array.
  1 commentaire
Tony
Tony le 24 Avr 2016
Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by