I am trying to make a temperature conversion table but when I print at the end, all my values are jumbled. How can I seperate the calculations and label the results for each?
bF = input('Input the beginning temperature in Fahrenheit');
eF = input('Input the endung temperature in Fahrenheit');
disp('Temperature in Fahrenheit & Celcuis')
F = [bF:10:eF];
for k = 1
C = (F-32)*(5/9);
end
fprintf('%8.1f\t%8.1f\n',F.',C.')

 채택된 답변

Benjamin Kraus
Benjamin Kraus 2020년 12월 3일

0 개 추천

You need to merge F and C into a single matrix and transpose that matrix:
fprintf('%8.1f\t%8.1f\n',[F' C']')

댓글 수: 4

Benjamin Kraus
Benjamin Kraus 2020년 12월 3일
Note that you also don't need the for loop, it is not actually doing anything in your code.
You may also want to try compose, which handles vectors a little differently than fprintf, and the behavior when the input has multiple rows/columns is spelled out a little more clearly in the documentation.
fprintf('%s\n',compose("%8.1f\t%8.1f",F',C'))
George Amor
George Amor 2020년 12월 3일
Im trying to incorporate a for loop that fits this:
Use a for loop with index variable, F ,which is assigned a vector of Fahrenheit temperature values that were provided by the user.
o Each time through the loop, calculate the Celsius temperature value, C ,that corresponds to the index variable F .
Benjamin Kraus
Benjamin Kraus 2020년 12월 3일
As your code demonstrates, it is not necessary to use a for loop to perform the same computation on every element of a vector in MATLAB. You can do that in one line of code without a loop.
The requirement to use a loop looks like a homework requirement. You can do this using a for loop, but it isn't necessary, and your current code is not using the for loop correctly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

질문:

2020년 12월 3일

댓글:

2020년 12월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by