MATLAB Answers


what is wrong with this simple code?

Asked by Sergio
on 4 Jul 2013

Hi I am trying to create an empty array so that I can add elements to it concatenating them. This avoid the large time it takes if you dont give it a size previous the for loop, as it has to change its size at every iteration. This is the code:

>> days=double.empty(100000,1,0); m=size(days); m

for k=2:numel(UntitledCase_Temp)



m =

      100000           1           0

??? Attempted to access days(1); index out of bounds because numel(days)=0.



No products are associated with this question.

1 Answer

Answer by the cyclist
on 4 Jul 2013
 Accepted answer

Preallocate like this instead

days = zeros(100000,1);


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!