Pre-allocation issue non existent before
1 回表示 (過去 30 日間)
古いコメントを表示
Dear Matlabers,
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
data_pv_pu=dlmread('pv.txt');
t=data_pv_pu(:,1);
K=1;
P_pv_nominal=1500;
P_pv=K*P_pv_nominal;
data_pv=data_pv_pu(:,2)*P_pv;
data_load_0=dlmread('load_v.txt');
data_load=data_load_0(:,2);
V=400
Sum_without0=0
Sum_FV0=0
Sum_without=Sum_without0
Ac_saving=0;
Avg_saving=0;
K=1
for x=-99:100
for h=2:8760
P=data_load(h);
P_FV=data_pv(h)*K;
I=P/V;
I_FV=P_FV/V;
for r=1:100
Sum_without=Sum_without+(I*r)^2;
end
Sum_FV=Sum_FV0;
if x>=1
for r=1:100
if r<x
Sum_FV=Sum_FV+(I*r)^2;
else
Sum_FV=Sum_FV+((I*r)-I_FV)^2;
end
end
else
for r=1:100
Sum_FV=Sum_FV+((I*r)-I_FV)^2;
end
Sum_FV=Sum_FV-(x-1)*(I_FV)^2;
end
Ahorro=(Sum_without-Sum_FV)/Sum_without;
Ac_saving=Ac_saving+Ahorro; %
Ahorro=0;
Sum_without=0;
Sum_FV=0;
end
Avg_saving (101-x)=Ac_saving/8760;
Ac_saving=0;
end
plot(Avg_saving)
set(gcf,'Visible','on')
hold on
0 件のコメント
回答 (1 件)
SungJun Cho
2021 年 4 月 15 日
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
Ac_saving=zeros(1,100);
Avg_saving=zeros(1,100);
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.
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!