Index exceeds the number of array elements (1).

Hi guys , how can i fix this
cost_benefit=[];
controller_cost=1;
k=4;
for i=1:k
cost_benefit(1,i)=(controller_cost*i)/(L_average(i));
end
%
k=[1 2 3 4];
figure;
AxesH = axes('Xlim', [1, 4], 'XTick', 1:1:4, 'NextPlot', 'add');
line(k,cost_benefit,'MarkerSize',20,'Marker','.','Color','b')
hold on
box on;
plot(k,cost_benefit, '-b')
set(gca,'fontsize',20)
% title('Optimal number of controllers based on cost benefit');
xlabel ('Number of controllers','FontSize',30, 'FontWeight','bold')
ylabel ('Cost benefit ($/ms)','FontSize',30,'FontWeight','bold')
where the error apears in this line
cost_benefit(1,i)=(controller_cost*i)/(L_average(i));
Thank you all.

Answers (1)

You can follow the steps provided in this answer to determine the issue.

2 Comments

I followed the answer you provided but i can not figure out how fix it
Unfortunately, you haven't shared enough details if your post for us to help you, either.
Try using the steps I shared to inspect what is happening with L_average(i)
To get started, try running the following.
L_average(1)
L_average(2)
L_average(3)
L_average(4)
You may find the Array Indexing page helpful.

Sign in to comment.

Asked:

on 18 Jan 2021

Edited:

on 25 Jan 2021

Community Treasure Hunt

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

Start Hunting!