필터 지우기
필터 지우기

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

채택된 답변

VBBV
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
VBBV 2021년 2월 2일
Notice that the problem doesn't persist when you use the for loop approach presented

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

추가 답변 (1개)

Steven Lord
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)
1 2 3 4 5 6 7 8 9 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])
1 6 2 7 3 8 4 9 5 10
  댓글 수: 2
Sophine Teng
Sophine Teng 2021년 2월 2일
Hi Steven, this methods works unless you know the number of elements to output beforehand?
Correct me, if i am wrong.. I am just a beginner at it.
Steven Lord
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)
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
r = reshape(M, 1, []) % 1 row by MATLAB-you-figure-it-out columns
r = 1×16
16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by