Filling in 8x8 matrix with formula

31 vues (au cours des 30 derniers jours)
Dani Matso
Dani Matso le 15 Oct 2020
Commenté : KSSV le 16 Oct 2020
I want to create a 8x8 matrix and fill in the values using a formula. This is the workflow I have in mind is to create some nested for loop that does this:
  1. I create a empty matrix A using A = zeros(8,8)
  2. I create variables p(i) using (pi/16)+(i-1)*(pi/8)
  3. I create a(i,j) = cos((j-1)*p(i)) to fill in matrix
This is what I have tried:
A = zeros(8,8)
for i = 1:8
p(i) = (pi/16)+(i-1)*(pi/8)
for i = 1:8
for j = 1:8
A(i,j) = cos((j-1)*p(i))
Thank you

Réponse acceptée

KSSV
KSSV le 15 Oct 2020
Modifié(e) : KSSV le 15 Oct 2020
A = zeros(8,8)
for i = 1:8
p = (pi/16)+(i-1)*(pi/8)
for j = 1:8
A(i,j) = cos((j-1)*p)
end
end
As the input values of cos are very small...all of them are 1....check your formula is it correct?
  2 commentaires
Dani Matso
Dani Matso le 16 Oct 2020
Modifié(e) : Dani Matso le 16 Oct 2020
Thanks a lot for helping KSSV! Is the (i) in p not necessary in the code? Because there are eight different p's.
The formula for p is:
pi = π/16 + (i − 1)π/8 , for i = 1, 2, . . . , 8. So I get: p1 = π/16 , p2 = 3π/16 , · · · , p8 = 15π/16
Secondly:
The matrix A(i,j) is given by cos((j-1)pi)
I think your code implements this correctly, if you have no corrections based on my new information I will mark this as the accepted answer:-)
KSSV
KSSV le 16 Oct 2020
A = zeros(8,8)
for i = 1:8
p = (pi/16)+(i-1)*(pi/8)
for j = 1:8
A(i,j) = cos((j-1)*p)
end
end
See that as you wanted, the p is changing the value...it follows as you said.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by