Pre-allocation Warnings, how can I stop this warning?

8 次查看(过去 30 天)
Hi, I have this code, and is getting warnings about pre-allocation to speed up in this variables:numdia,mediasDiarias, how can I pre-allocate this 2 variables, to stop the warning, this is driving my crazy, because I hate warnings, and I tried a lot to the warning disappear.
function [mediasDiarias,mesano,numdia]=perfilhorariosmensal(valores,ano,mes,hor)
mediasDiarias = [];
numdia = [];
mesano{1} = 'HORAS';
contMeses =2;
k=1;
while k<size(valores,1)
som = zeros(24,1);
cont = zeros(24,1);
if ~isnan(valores(k))
som(hor(k)+1) = valores(k);
cont(hor(k)+1) = 1;
end
k=k+1;
while ( ano(k) == ano(k-1) ) && ( mes(k) == mes(k-1) )
if ~isnan(valores(k))
som(hor(k)+1) = som(hor(k)+1) + valores(k);
cont(hor(k)+1) = cont(hor(k)+1) + 1;
end
k=k+1;
if k == size(valores,1)
break;
end
end
mesano{contMeses} = [n2s(mes(k-1),'%02d') '/' n2s(ano(k-1),'%04d')];
%mediasDiarias = [mediasDiarias som];%./cont];
mediasDiarias = [mediasDiarias som./cont];
numdia = [numdia (sum(cont)/24)];
contMeses=contMeses+1;
%pause
end
end

采纳的回答

vik
vik 2019-2-8
You just need to follow the warning and pre-allocate the memory and make some small changes to your code.
Instead of putting togehter arrays by using numdia = [numdia sum(cont)/24] which will change the numdia-array every iteration, always pre-allocate it first by assigning a zero-array which has the exact size your array should have at the end and use indexing then to assign values.
Basically you just need to know which size the array will have at the end of all your loops and assign it first.
numdia = zeros(1,size(valores,1)); % Pre-allocate memory by assigning a zero-array first
for k = 1:size(valores,1)
% ... more code here ...
numdia(k) = sum(cont)/24
end
  1 个评论
Kiran Jojare
Kiran Jojare 2020-10-10
Hi vik,
Im getting the same error on my cell array.
But how can I preallocate a 1*5 cell array ?
I tried using cell. But the warning didnt go?
Looking forward for your reply. Thank you in advance Here is my code.
clc;clear all;workspace;format long g; format compact;fontSize =10;
g = -9.81;v0 = 10; y0 = 0 ;x0 = 0;angle = 90;xFinal = zeros(1,5);
Computation
v0x = v0*cosd(angle);
v0y = v0*sind(angle);
a = (1/2) * g;b = v0y;c = y0;
tFinal = roots([a, b, c]);
tFinal = max(tFinal);
t = linspace(0, tFinal, 1000);
counter = 1;legends = {};
for angle = 15: 15 : 75
v0x = v0*cosd(angle);
v0y = v0*sind(angle);
x = x0 + v0x * t;
y = y0 + v0y * t + (1/2) * g * t .^ 2;
y(y < 0) = 0;
indexHitGround = find(y > 0, 1, 'last');
plot(x, y, '-', 'LineWidth', 2);
hold on;
legends{end+1} = sprintf('Angle = %d', angle);
xFinal(counter) = x(indexHitGround);
counter = counter + 1;
end
grid on;
legends{1,1}
whos ans
xlim([0,max(xFinal)+2])
xlabel('X Coordinate', 'FontSize', fontSize);
ylabel('Y Coordinate', 'FontSize', fontSize);
title ('Projectile Trajectory', 'FontSize', fontSize);
legend(legends)
Note :Im getting warning on legends

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by