So I have to calculate the electricity usage for a set of values 200 500 700 1000 1500. I have written the program
x=input('Units of Electricity used ');
if x<500
y=x*0.02 + 5;
elseif x>500 && x<1001
y= 15 + (.05*(x-500));
elseif x>1000
y= 40 + (.1*(x-1000));
end
T=table(x,y);
T.Properties.VariableNames={'ElectricityUnits','PriceInPounds'};
disp(T)
which will show me the correct values for the price of the units, however only if I enter the values in separately and then re run the code.
Is there a way for me to put them all in at once creating a larger table? e.g setting a variable to =[200 500 700 1000 1500]?
also is there a way to have the titles of the columns have spaces in?
thank you!

 Akzeptierte Antwort

Birdman
Birdman am 30 Okt. 2017

0 Stimmen

x=input('Units of Electricity used ');
for i=1:length(x)
if x(i)<500
y(i)=x(i)*0.02 + 5;
elseif x(i)>500 && x(i)<1001
y(i)= 15 + (.05*(x(i)-500));
elseif x>1000
y(i)= 40 + (.1*(x(i)-1000));
end
end
T=table(x,y);
T.Properties.VariableNames={'ElectricityUnits','PriceInPounds'};
disp(T)
Enter input as
[200 500 700 1000 1500]

1 Kommentar

chris w
chris w am 31 Okt. 2017
ah perfect thank you!
Quick question how come the table gets displayed like so:
ElectricityUnits PriceInPounds
___________________________________ _________________________
200 500 700 1000 1500 9 15 25 40 90
rather than like
200 9
500 15
700 25
etc?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 30 Okt. 2017

Kommentiert:

am 31 Okt. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by