Pre-allocation issue non existent before
3 views (last 30 days)
Show older comments
Edited: Manyu Hati on 15 Apr 2021
I would like to solve a pre-allocation problem in my matlab code. This code used to run in 2013 without issues. However, I have tried to run it again, and now Matlab points out that the code has a pre-allocation issue. Please, find attached the data files and the code below to run the simulation. I have tried to fix it by pre-allocating Ac_saving=zeros(100); and Avg_saving=zeros(100), without luck, as Matlab says that the index exceeds the number of elements. Could you please help me to repair the code? Many thankss
SungJun Cho on 15 Apr 2021
First of all, when I ran your code, Avg_saving outputs 1 x 200 double array, so you might want to preallocate the arrays in right size.
Based on your code, it seems like you want to preallocate the arrays. If that is the case, try using
instead of just zeros(100), since the latter will give you 100 x 100 matrix instead of 1 x 100 vector array.
Hope this helps.
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!