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

11 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

vik
vik il 8 Feb 2019
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 Commento
Kiran Jojare
Kiran Jojare il 10 Ott 2020
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by