Azzera filtri
Azzera filtri

Issue with making an array

1 visualizzazione (ultimi 30 giorni)
Tony
Tony il 23 Apr 2016
Commentato: Tony il 24 Apr 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?

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by