How do I create a matrix with a sine and cosine function?
Show older comments
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
More Answers (1)
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()
2 Comments
Shruba Gangopadhyay
on 5 Feb 2018
use i=[1:10] saves some typing time
Stephen23
on 5 Feb 2018
@Shruba Gangopadhyay: the square brackets are not required:
i = 1:10;
is faster and simpler.
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!