필터 지우기
필터 지우기

Issue with making an array

조회 수: 1 (최근 30일)
Tony
Tony 2016년 4월 23일
댓글: Tony 2016년 4월 24일
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?

채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 23일
Approximation_b = zeros(1, 30000);
You were creating a 30000 by 30000 array.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by