필터 지우기
필터 지우기

Why am I getting an indices error?

조회 수: 2 (최근 30일)
Fatemah Ebrahim
Fatemah Ebrahim 2020년 6월 8일
답변: Walter Roberson 2020년 6월 8일
clc;
clear;
data = readtable('A_minute.xlsx');
% step 2: store date and time data as a datenum (DaT) and then convert to datetime (t)
% summer: t(2:102241)
DaT = datenum(data.Var1);
t = datetime(DaT, 'ConvertFrom','datenum');
TimeofDay = timeofday(t(2:102241));
TimeofDay = ceil(TimeofDay,'minute');
[UniqueTimeSteps,~,pos] = unique(TimeofDay);
UniqueTimeSteps = UniqueTimeSteps(2:end);
pos =pos -1;
pos(pos==0) = 24;
% step 3: store the other columns from the excel data file
AptA = data(:,4);
AptA = table2array(AptA);
AptA = AptA(1:102241);
% step 4: for loop extracting the hour and indexing it through for weekday versus weekend
Consumption_Weekday = zeros(1,length(UniqueTimeSteps));
Consumption_Weekend = zeros(1,length(UniqueTimeSteps));
wd = weekday(t) > 1 & weekday(t) < 7;
for i = 1:length(UniqueTimeSteps)
idx = (pos == i & wd);
Consumption_Weekday(i) = mean(AptA(idx));
idx = (pos == i & (~wd));
Consumption_Weekend(i) = mean(AptA(idx));
end
MinuteValue= UniqueTimeSteps;
% step 5: plot
figureA = figure;
plot(MinuteValue,Consumption_Weekday,'-b');
hold on
plot(MinuteValue,Consumption_Weekend,'-r');
ylim([0.2 0.7]);
xlabel('Time'), ylabel('Electric Demand (kW)'), ...
title('Unit A - Average Aggregated HVAC Electric Demand (kW)'), ...
legend('Weekdays','Weekends')
Matrix dimensions must agree.
Error in A_minute_wk (line 30)
idx = (pos == i & wd);
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 6월 8일
You did not indicate which line was causing the problem, but observe that
idx = (A_t.Minute == MinValue & weekday(A_t) > 1 & weekday(A_t) < 7);
could potentially match several entries, so AptAs(idx) might be multiple values (or no values?), but you expect it to be a scalar.
Fatemah Ebrahim
Fatemah Ebrahim 2020년 6월 8일
I just edited the question, please see above

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 6월 8일
[UniqueTimeSteps,~,pos] = unique(TimeofDay);
pos will be a vector with as many entries as TimeofDay has.
pos =pos -1;
pos(pos==0) = 24;
Those do not change the size of pos.
t = datetime(DaT, 'ConvertFrom','datenum');
TimeofDay = timeofday(t(2:102241));
TimeOfDay will be exactly 102240 elements long (or else you would have gotten an error at that step).
TimeofDay = ceil(TimeofDay,'minute');
That did not change the size of TimeOfDay.
So we know that TimeOfDay will have 102240 elements, and since pos is the same size, pos will have 102240 elements.
wd = weekday(t) > 1 & weekday(t) < 7;
We do not know (without knowing the size of input) how large t is. But we can guarantee that it is larger than TimeOfDay, because TimeOfDay is derived from something that starts at element 2 of t. t might potentially end at 102241, but that would make it 1 element larger than TimeofDay.
idx = (pos == i & wd);
As described above, those are certain to be different sizes.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by