How do I create a matrix with a sine and cosine function?

8 views (last 30 days)
Individual elements of B are referenced using B[i,j], where i is the row and j is the column. Define each element within B as B[i,j] = sin(i) cos(j) where both i and j go from 1 to 10 [Hint: B[1,1]=sin(1)*cos(1)]. It's size has to be 10x10.
I know that to define i and j I have to type in the code:
i=[1,2,3,4,5,6,7,8,9,10];
j=[1,2,3,4,5,6,7,8,9,10]
Also when I'm creating the function B I know that I have to put the code:
B=sin(i).*cos(j)
But how do I repeat to create the matrix 10x10?
Thank you so much! :)

Accepted Answer

Stephen23
Stephen23 on 24 Nov 2016
Edited: Stephen23 on 24 Nov 2016
>> V = 1:10;
>> B = sin(V.')*cos(V)
B =
0.454649 -0.350175 -0.833050 -0.550022 0.238693 0.807955 0.634387 -0.122434 -0.766690 -0.706054
0.491295 -0.378401 -0.900198 -0.594356 0.257933 0.873080 0.685521 -0.132303 -0.828488 -0.762966
0.076247 -0.058727 -0.139708 -0.092242 0.040030 0.135499 0.106391 -0.020533 -0.128579 -0.118410
-0.408902 0.314941 0.749229 0.494679 -0.214676 -0.726659 -0.570555 0.110115 0.689546 0.635011
-0.518109 0.399053 0.949328 0.626795 -0.272011 -0.920731 -0.722935 0.139524 0.873705 0.804606
-0.150969 0.116278 0.276619 0.182638 -0.079260 -0.268286 -0.210652 0.040655 0.254584 0.234450
0.354971 -0.273403 -0.650412 -0.429435 0.186362 0.630819 0.495304 -0.095592 -0.598600 -0.551259
0.534553 -0.411718 -0.979457 -0.646688 0.280644 0.949952 0.745879 -0.143952 -0.901434 -0.830142
0.222669 -0.171502 -0.407994 -0.269379 0.116902 0.395704 0.310697 -0.059963 -0.375494 -0.345797
-0.293936 0.226393 0.538577 0.355596 -0.154318 -0.522353 -0.410139 0.079155 0.495674 0.456473

More Answers (1)

Walter Roberson
Walter Roberson on 24 Nov 2016
Edited: Walter Roberson on 24 Nov 2016
In R2016b, you can just do
i=[1,2,3,4,5,6,7,8,9,10];
j=[1,2,3,4,5,6,7,8,9,10];
B = sin(i) .* cos(j).';
In earlier releases, see bsxfun() or repmat() or meshgrid() or ndgrid()

Community Treasure Hunt

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

Start Hunting!