hello i need to write script mathlab to make a matrix with vector v=[1 2 3 4]

2 vues (au cours des 30 derniers jours)
the output is [1 1 1 1;2 4 8 16;3 9 27 81;4 16 64 256]
  2 commentaires
Mohamad  Haj
Mohamad Haj le 11 Déc 2021
Thank you for help a=[1,2,3,4]; for i=2:4 a(i,:)=a((i-1),:).^2
end i do this code but there is an error in it and I don't know where

Connectez-vous pour commenter.

Réponse acceptée

Awais Saeed
Awais Saeed le 11 Déc 2021
Seems you want to have powers from 1 to 4
v = 1:4;
bsxfun(@power,v(:),[1:4]) % poers ranging from 1 to 4
ans = 4×4
1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256
  6 commentaires
Awais Saeed
Awais Saeed le 11 Déc 2021
  • Take transpose of vector a to make it a row vector.
  • Take square of a and put the result in second column
  • Take cube of a and put the result in third column.
  • Use index of for loop with .^ to take square, cube, etc

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by