index exceed matrix elements

1 次查看(过去 30 天)
Tasneem Abed
Tasneem Abed 2023-7-11
评论: Voss 2023-8-15
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)
power = 2×1
3000 1800
Dt=data(:,2) - data(:,1)
Dt = 2×1
6 3
Power_Generated=power.*Dt
Power_Generated = 2×1
18000 5400
Total_power1=sum(Power_Generated)
Total_power1 = 23400
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));
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
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

回答(2 个)

Voss
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
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

类别

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