How to display a set of answers as a vector?

6 views (last 30 days)
Graditz
Graditz on 8 Feb 2016
Commented: Graditz on 9 Feb 2016
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

Answers (1)

Walter Roberson
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);
  1 Comment
Graditz
Graditz on 9 Feb 2016
Thanks! However, this seems to spit out tc in 'increment' vectors, rather than just one vector with the same length as Tf. Is there a way to just display the final vector?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!