Part b) Display the content of “log10(A)” on the screen using “sprintf” in the following format: “A is [1 10 100 1000]. log10(A) calculated using the natural logarithm function is [0 1 2 3].” In the format above, where the numbers are bold, the values of “A” and “log10(A)” must be used instead of simply typing the numbers. In other words, do not simply type in the entire line above between two quotation marks of “sprint”, but use a format similar to the following: sprint(‘A is [ %? ]. log10(A) calculated using the natural logarithm function is [ %?]’ ,A,?) What you need to find is the format to display “A” and “log10(A)”, e.g. %u, %d, %s etc (the first and second question marks) and the function you defined in part (a) for the third question mark.

 채택된 답변

Star Strider
Star Strider 2015년 1월 29일

1 개 추천

See the documentation for sprintf.

댓글 수: 5

Matthew Liao
Matthew Liao 2015년 1월 29일
so your saying I have to type sprint(‘A is [ 1 10 100 1000 ]. log10(A) calculated using the natural logarithm function is [0 1 2 3]’ ,A,?).
but when I type this is giving me an error saying
Error: Unbalanced or unexpected parenthesis or bracket.
Image Analyst
Image Analyst 2015년 1월 29일
편집: Image Analyst 2015년 1월 29일
No, he said to look up the documentation for sprintf so you know that the four numbers get replaced by %f (four of them) and the variables themselves are listed after the format string
message = sprintf('A is [%f %f.............', A, ......);
See if you can fill in the missing parts for your homework. I can't just do the whole homework outright for you of course.
Please show your actual code. The function is sprintf, so ‘sprint’ will throw an error. You will need to use the necessary format descriptors (such as %f) to output your numbers.
For example:
Q = [3:5];
sprintf('Q = [%.1f %.1f %.1f]', Q)
produces:
Q = [3.0 4.0 5.0]
Matthew Liao
Matthew Liao 2015년 1월 29일
logarithmic = sprintf('A is [%f %f %f %f], log10(A) calculated using the natural logarithm function is [%u %u %u %u]',A,C)
which works but when I did it this way it gave me some weird numbers
logarithmic = sprintf('A is [%u %u %u %u]',A,'log10(A) calculated using the natural logarithm function is [%u %u %u %u]',C)
logarithmic =
A is [1 10 100 1000]A is [108 111 103 49]A is [48 40 65 41]A is [32 99 97 108]A is [99 117 108 97]A is [116 101 100 32]A is [117 115 105 110]A is [103 32 116 104]A is [101 32 110 97]A is [116 117 114
Star Strider
Star Strider 2015년 1월 29일
편집: Star Strider 2015년 1월 29일
I would use ‘%f’ for the natural logarithm function output as well. The ‘%u’ is for base 10 unsigned integers, and will produce strange results.
This works perfectly for me:
A = [1 10 100 1000];
C = log(A);
logarithmic = sprintf('A is [%f %f %f %f], log10(A) calculated using the natural logarithm function is [%f %f %f %f]',A,C)
and produces:
logarithmic =
A is [1.000000 10.000000 100.000000 1000.000000], log10(A) calculated using the natural logarithm function is [0.000000 2.302585 4.605170 6.907755]

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

추가 답변 (0개)

카테고리

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

질문:

2015년 1월 29일

편집:

2015년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by