index exceed matrix elements
1 次查看(过去 30 天)
显示 更早的评论
i want data=[9 15 3000;
15 18 1800];to replace the one below
% data=[0 6 900;
% 6 7 2400;
% 7 15 3000;
% 15 18 1800;
% 18 22 3000;
% 22 24 1800];
data=[9 15 3000;
15 18 1800];
power=data(:, 3)
Dt=data(:,2) - data(:,1)
Power_Generated=power.*Dt
Total_power1=sum(Power_Generated)
Total_power2=power.*Dt;
DATA=[data(:,1) data(:,2) power];
Average_load=Total_power2/sum(Dt);
peak_load=max(power);
Daily_LF=Average_load/peak_load*100;
Results=[Average_load peak_load*ones(size(Daily_LF)) Daily_LF];
L=length(data);
timeinterval=data(:,1:2) ;
t = sort(reshape(timeinterval,1,2*L));
for n = 1:L
P(2*n-1) = power(n);
P(2*n) = power(n);
end
subplot(2,1,1)
plot(t,P,'b')
xlabel(['Timer, Hr'])
ylabel(['Power, W'])
title('consumed Power, W versus time, hour')
xlim([0 24])
grid on
grid minor
0 个评论
回答(2 个)
Voss
2023-7-11
Replace this:
L=length(data);
with this:
L=size(data,1);
length(data) gives the size of the longest dimension of data, which is 6 when data is 6-by-3 but is 3 when data is 2-by-3. What you really want is the size of the first dimension, i.e., the number of rows of data, so use size(data,1).
Mathieu NOE
2023-7-11
Code improved below
same mistake found as @Voss
% data=[0 6 900;
% 6 7 2400;
% 7 15 3000;
% 15 18 1800;
% 18 22 3000;
% 22 24 1800];
data=[9 15 3000;
15 18 1800];
power=data(:, 3);
Dt=data(:,2) - data(:,1);
Power_Generated=power.*Dt;
Total_power1=sum(Power_Generated);
% Total_power2=power.*Dt; % same as 2 lines above : Power_Generated=power.*Dt;
% DATA=[data(:,1) data(:,2) power]; % what for ? this is exactly the same
% as array "data" at the beginning of your code
% Average_load=Total_power2/sum(Dt);
Average_load=Power_Generated/sum(Dt);
peak_load=max(power);
Daily_LF=Average_load/peak_load*100;
Results=[Average_load peak_load*ones(size(Daily_LF)) Daily_LF];
L=size(data,1); % get number of rows
timeinterval=data(:,1:2) ;
t = sort(reshape(timeinterval,1,2*L)); % this works
% t = sort(timeinterval(:)); % this works too ! (and is simpler to write)
for n = 1:L
P(2*n-1) = power(n);
P(2*n) = power(n);
end
subplot(2,1,1)
plot(t,P,'b')
xlabel(['Timer, Hr'])
ylabel(['Power, W'])
title('consumed Power, W versus time, hour')
xlim([0 24])
grid on
grid minor
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!