How to display the correct output of the product of matrix correctly when i wanted to conver degrees to radians
2 ビュー (過去 30 日間)
古いコメントを表示
hi all, below is my code and I am trying to display the result of a matrix mutiplication correctly. Does reshaping of matrix or contencation of string?
prompt = 'Enter the numbers for conversion? ';
x = input(prompt)
y = x*(pi./180); % formula to convert degrees to radians
fprintf("%d degree\n = %d radians\n", x ,y);
my output is as follows:
What are the number/numbers for conversion? [180 360]
180.000 degree
= 360.000 radians
3.142 degree
= 6.283 radians
>>
The correct display i wanted is 180 degrees = 3.142 , 360 degrees = 6.283 radians
0 件のコメント
採用された回答
VBBV
2021 年 2 月 2 日
編集済み: VBBV
2021 年 2 月 2 日
prompt = 'Enter the numbers for conversion? ';
x = input(prompt)
y = x*(pi./180); % formula to convert degrees to radians
fprintf("%d \t %d degree\n = %d \t %d radians\n", x ,y);
9 件のコメント
VBBV
2021 年 2 月 2 日
Notice that the problem doesn't persist when you use the for loop approach presented
その他の回答 (1 件)
Steven Lord
2021 年 2 月 2 日
編集済み: Steven Lord
2021 年 2 月 2 日
The fprintf function will iterate through the elements of the second input you pass to it when determining how to fill the formatting operators. Once all the elements of the second input has been used it will move to the third, fourth, etc.
fprintf('%d %d\n', 1:5, 6:10)
If you want it to alternate you can concatenate the second and later inputs into an array:
fprintf('%d %d\n', [1:5; 6:10])
2 件のコメント
Steven Lord
2021 年 2 月 2 日
By the way you've created x and y you know they're the same size. In order to concatenate them like I did, you need them to be row vectors. You can make them rows using reshape.
M = magic(4)
r = reshape(M, 1, []) % 1 row by MATLAB-you-figure-it-out columns
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!