pls help me fix this i trying to find the earning and savings of it trough loops but it say the index exceed numbers of array

2 ビュー (過去 30 日間)
savings=[];%initial saving vectors
earnings=zeros(1,1212);%initial earning vectors
T_earnings_Jul=0;%initial total earning vectors up to jul
earnings_Aug=zeros(1,293);%initial earning vectors up to Aug
T_earnings_Aug=0;%initial total earning vectors up to Aug
total_earning=0;
% Loop through each day to calculate savings and earnings
for i = 1:1505 % total of 1505 day
savings(i) = O_Consumption(i).*Cost(i);
end
%from April 06 2020
for i =1:1212
earnings(i)=e_out(i).*Feed(i);
T_earnings_Jul=T_earnings_Jul+earnings(i);
end
%from auguest 1 2023
for i= 1213:1515
%less than 10kwh
if e_out(i)<=10000
earnings(i)=e_out(i).*Feed(i);
T_earnings_Aug=T_earnings_Aug+earnings(i);
end
%more than 10kwh
if e_out(i)>10000
earnings_Aug(i)=((e_out(i)-10)./100)+0.88;
T_earnings_Aug=T_earnings_Aug+earnings_Aug(i);
end
end
total_earning=T_earnings_Jul+T_earnings_Aug;
fprintf('\n\nthe total savings earned is $%.2f\n',sum(savings));
fprintf('\n\nthe total earnings earned is $%.2f\n',sum(total_earning));
code is above
Index exceeds the number of array elements. Index must not exceed 1505.
Error in Q2 (line 52)
if e_out(i)<=10000
error is above

採用された回答

Sameer
Sameer 2024 年 10 月 9 日
編集済み: Sameer 2024 年 10 月 9 日
Hi Elisa
The error you are encountering is due to the loop index exceeding the number of elements in the array. The loop is iterating from 1213 to 1515, but your array "e_out" only has 1505 elements.
You can change the loop as follows:
for i = 1213:1505
To avoid indexing errors, you can use MATLAB's built-in "length" function to determine the size of your arrays dynamically.
Hope this helps!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by