Maximum variable size allowed by the program is exceeded and some errors

5 次查看(过去 30 天)
Hi Guys,
i want to write simple moving average in matlab by my code. i know there is and function for this reason but i need to write it. my first error is,Maximum variable size allowed by the program is exceeded . can someone fix it for me.
i have one function including SMA and one script.
Thank you in Advanced.
The SMA function is
function Simple_M=SMA(Buffer2,period,bar)
sum1=zeros(1,period);
for i=1:period
sum1=sum1+Buffer2(bar-i);
end
Simple_M=sum1/period;
end
The Script is
clc;
clear;
close all;
close1=[11 34 82 22 43 26 98 73 81 21 55 39 76 15 45 68 39 28 11 28 44];
Period=7;
Buffer=zeros(1:20);
for i=10:20
Buffer(i)=SMA(close1(i),Period,i);
end

回答(1 个)

Steven Lord
Steven Lord 2020-6-7
Buffer=zeros(1:20);
You need to use a comma instead of a colon.
BufferComma=zeros(1,20);
BufferComma is a 1-by-20 vector. Buffer would be a 1-by-2-by-3-by-4-by-5-by-6-by-... -by-19-by-20 array. How much memory would Buffer require?
>> numberOfElements = prod(1:20);
>> bytes = 8*numberOfElements;
>> exabytes = bytes/1e18
exabytes =
19.4632160654131
How much memory is that? According to the Wikipedia page for exabyte "The content of the Library of Congress is commonly estimated to hold 10 terabytes of data in all printed material. Recent estimates of the size including audio, video, and digital materials start at 3 petabytes[27] to 20 petabytes. Therefore, one exabyte could hold a hundred thousand times the printed material or 50 to 300 times all the content of the Library of Congress."
Your computer doesn't have that much memory, and even if it did numberOfElements is greater than the maximum number of elements allowed in a matrix in MATLAB as stated by the computer function.
  4 个评论
mahan farahani
mahan farahani 2020-6-7
I found out the problem. close1(i) should be close. because its an array but close1(i) is only one of the parts of close1.
Thank you for your advising.
Walter Roberson
Walter Roberson 2020-6-7
It is more likely that close1(i) should be close1 not close . close is a MATLAB call to remove graphic windows.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by