Index out of bounds

1 次查看(过去 30 天)
David
David 2014-5-17
评论: David 2014-5-18
clc clear close
amount = input('What is the total resistance?: ');
fprintf('The total resistance is %0.f, so you will have in the series: \n', amount);
bill=[82*10^6,68*10^6,56*10^6,47*10^6,39*10^6,33*10^6,27*10^6,22*10^6,18*10^6,15*10^6,12*10^6,10*10^6,82*10^5,68*10^5,56*10^5,47*10^5,39*10^5,33*10^5,27*10^5,22*10^5,18*10^5,15*10^5,12*10^5,10*10^5,82*10^4,68*10^4,56*10^4,47*10^4,39*10^4,33*10^4,27*10^4,22*10^4,18*10^4,15*10^4,12*10^4,10*10^4,82*10^3,68*10^3,56*10^3,47*10^3,39*10^3,33*10^3,27*10^3,22*10^3,18*10^3,15*10^3,12*10^3,10*10^3,82*10^2,68*10^2,56*10^2,47*10^2,39*10^2,33*10^2,27*10^2,22*10^2,18*10^2,15*10^2,12*10^2,10*10^2,82*10,68*10,56*10,47*10,39*10,33*10,27*10,22*10,18*10,15*10,12*10,10*10,82,68,56,47,39,33,27,22,18,15,12,10];
r=amount;
tnb=0;
i=1;
while r~=0
nb(i)=floor(r./bill(i));
tnb=tnb + nb(i);
r=rem(r ,bill(i));
if nb(i) >= 1
fprintf('%.0f of resistors %.0f \n' ,nb(i) ,bill(i))
end
i=i+1;
end
fprintf(' %.0f resistors in total \n' ,tnb)
How do i make it so it stops at the ideal combination? eg, if i type in 18 it will return 18 with 1 resistor in total, but if i type 20, i want it to stop at 18 as well but the code seems to want to keep going but it becomes out of bounds, anyway of stopping this? :D

采纳的回答

Image Analyst
Image Analyst 2014-5-17
Try this:
while r~=0 && i <= length(bill)
and also see this.

更多回答(0 个)

类别

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