index exceed matrix elements
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Voss
on 11 Jul 2023
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
on 11 Jul 2023
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!