How do I create a matrix with a sine and cosine function?
Afficher commentaires plus anciens
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! :)
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 24 Nov 2016
Modifié(e) : Walter Roberson
le 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 commentaires
Shruba Gangopadhyay
le 5 Fév 2018
use i=[1:10] saves some typing time
Stephen23
le 5 Fév 2018
@Shruba Gangopadhyay: the square brackets are not required:
i = 1:10;
is faster and simpler.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!