use previous element in 5 dimensional matrix for equation in current cell

조회 수: 1 (최근 30일)
Hi I have a 5 dimensional matrix as shown in the code below. What I am trying to do is find the "battery_capacity" shown in the "if function" what uses amongst other variables, the "battery_capacity" value of the previous cell in the loop (i.e h-1). I know the "battery_capacity" at the first cell which is "battery_capacity(:,:,:,1,1).
The relevant part of the code is below and I get the error "Subscript indices must either be real positive integers or logicals."
I believe its due to the (h-1) format which I can't really find a solution to. Also I need to be able to separate the days and thats why I have the hours and days as two separate dimensions.
% number_of_days = 3;
number_of_hours = 24*number_of_days;
number_panels = 1:5;
for idx_number_panels = 1:length(number_panels) % range of PV panel units examined
for number_turbines = 0:1 % range of wind turbine units examined
for number_batteries = 1:2 % range of battery units examined
for h=1:24 %# hours
battery_capacity(:,:,1,1) = max_battery_capacity*number_batteries;
for d = 1:number_of_days %# which day
n = h + 24*(d-1);
% hourly_deficit_1(...,..., h, d)= Demand(n)-(PV_supply(n)... %
battery_capacity(idx_number_panels , number_turbines + 1,number_batteries, h,d) = battery_capacity(idx_number_panels , number_turbines + 1,number_batteries, h-1,d)+...
(hourly_total_RES(idx_number_panels, number_turbines + 1,number_batteries, h,d)-hourly_annual_demand(n))*charging_efficiency;code
end
Thank you

답변 (1개)

Kevin Claytor
Kevin Claytor 2012년 7월 27일
"The relevant part of the code is below and I get the error "Subscript indices must either be real positive integers or logicals.""
Yes, you have h ranging from 1:24, and call h-1 (ranges from 0->23) Matlab matrix indices start at 1 (not 0 as in C), so trying to access an element indexed by 0 is not allowed. To avoid this, a quick solution is to start at 2 instead of 1; (Example loops through the values of a vector (1D matrix if you will) x, and adds the previous value to the current one.)
x = [45, 34, 14, 65, 12]
for ii = 2:5
fprintf('ii = %d \t x(ii) + x(ii - 1) = %d', ii, x(ii)-x(ii-1))
end
  댓글 수: 2
Andrew Alkiviades
Andrew Alkiviades 2012년 7월 27일
Thanks Kevin. I have tried your suggestion (if Ive understood correctly this is to start the loop for "h" from 2:24? This gives me the error "Index exceeds matrix dimensions" now
Andrew Alkiviades
Andrew Alkiviades 2012년 7월 27일
also do you agree with the following code for the very first element "battery_capacity(:,:,:,1,1) = 5;" ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by