How should I write a script that generates a table containing the sin and cos values for X between 0° and 90°, in 1° increments?
Ältere Kommentare anzeigen
I am a fairly new user to matlab and need some help creating this table. There should be three columns labeled x, sin(x), and cos(x) and 91 rows for the values of x (0 through 90), sin(x), and cos(x). Thanks!
2 Kommentare
Les Beckham
am 24 Sep. 2015
What have you tried so far? Are you getting some error messages? What are they?
The folks that answer questions on Matlab Answers are happy to help when a question indicates that you have really tried to solve a problem and need help with specific issues. Asking them to do the work for you is not likely to get you much help. These people are busy, too.
Marc Mezzacappa
am 25 Sep. 2015
Bearbeitet: Marc Mezzacappa
am 25 Sep. 2015
Antworten (2)
Image Analyst
am 25 Sep. 2015
You need to make all columns in your table column vectors not row vectors. Try it this way:
x = (0 : 90)'; % Transpose row vector to form column vector.
y1 = sin(x);
y2 = cos(x);
t = table(x, y1, y2)
Now x is a column vector and consequently y1 and y2 will be also, and since they all have the same number of rows, 91, table() will be happy.
3 Kommentare
Image Analyst
am 25 Sep. 2015
By the way, column names need to follow the same rules as any variable, that is, no spaces, can't start with a number, no special characters like ( or ), etc.
If you want column headers that have parentheses, you'll have to use fprintf() and print the header line manually, then loop over all the rows using fprintf() to print out each row one at a time.
Shannon Wagoner
am 23 Feb. 2020
can you go into detail on using fprintf to get column headers with more formatted appearance?
Image Analyst
am 23 Feb. 2020
for example:
fprintf(' x y \n--------------------\n');
Image Analyst
am 24 Sep. 2015
Bearbeitet: Image Analyst
am 24 Sep. 2015
Some hints:
x = 0 : 90;
threeColumntable = table(col1Vector, col2Vector, col3Vector);
and look at the functions sind() and cosd(). No for loop is needed - sind() can accept a whole vector of a bunch of numbers, not only one single scalar number.
Kategorien
Mehr zu Tables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!