필터 지우기
필터 지우기

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

조회 수: 8 (최근 30일)
Paulo Oliveira
Paulo Oliveira 2019년 2월 8일
댓글: Kiran Jojare 2020년 10월 10일
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