How to display a set of answers as a vector?
6 views (last 30 days)
Show older comments
When I try to the use the function below, for example convertTemp(20,25), Tc is not displayed as a vector, but rather six separate lines of Tc=...
Is there something i'm missing or is there a way to turn these outputs into a vector? Thanks.
function [Tf,Tc] = convertTemp(Tf_low,Tf_high)
% function [Tf,Tc] = convertTemp(Tf_low,Tf_high)
if Tf_low>Tf_high
disp('Warning: Invalid temperature input ')
Tf=-1
Tc=-1
end
for k=Tf_low:Tf_high
Tc=(k-32)./1.8
Tf=Tf_low:Tf_high;
end
end
0 Comments
Answers (1)
Walter Roberson
on 8 Feb 2016
Tf = Tf_low:Tf_high;
for Tfidx = 1 : length(Tf)
k = Tf(Tfidx);
Tc(Tfidx) = (k-32)./1.8;
end
After this, Tc will be a vector the same length as Tf
Note: I constructed this code in such a way that it does not depend upon Tf containing only integer values. It would, for example, still work if you defined
Tf = linspace(Tf_low, Tf_high, 20);
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!