フィルターのクリア

Having Trouble With Preallocating

1 回表示 (過去 30 日間)
Bailey Smith
Bailey Smith 2018 年 6 月 18 日
編集済み: Stephen23 2018 年 6 月 18 日
When I run my code:
clear; clc;
tic
x=(0:pi/1000000:pi);
m=zeros(1000000);
y=sin(x);
n=1;
L=length(x);
while n<L
m(n,1)=x(n);
m(n,2)=(y(n+1)-y(n))/(x(n+1)-x(n));
n=n+1;
end
m(n,1)=x(n);
m(n,2)=NaN;
toc
I get an error stating: Error using zeros Requested 1000000x1000000 (7450.6GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
What should I do about this? I would like to keep the while loops in there and just preallocate if possible. Thanks!

採用された回答

Stephen23
Stephen23 2018 年 6 月 18 日
編集済み: Stephen23 2018 年 6 月 18 日
m = zeros(1000000,2);
Although a better idea would be to preallocate with the exact required size:
L = numel(x);
m = zeros(L,2);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by