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

the output is [1 1 1 1;2 4 8 16;3 9 27 81;4 16 64 256]

2 commentaires

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

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

Note that this was a homework question. It would have been better to guide the poster instead of giving them the entire answer.
Yes I realized that after seeing your comment. I had my answer window opened up while doing other stuff on other browser tabs. Its strange that people do not even try to google the problem.
Thank you for you answer
a=[1,2,3,4];
for i=2:4
a(i,:)=a((i-1),:).^2
end
i do this code but need editing can you help me
Think about row 2, when i = 2. a(i-1,:) would be [1,2,3,4] . You would square that to get [1, 4, 9, 16] -- okay, that's the result you want in the second column.
Now think about row 3, when i = 3. a(i-1,:) would be [1,4,9,16]. You would square that to get [1,16,81,256]. Okay, that's the result you want in the 4th column.
But... how can you get the result you want for the third column? 27 is not the square of any integer, so you cannot get it using the technique you are using.
  • 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

Community Treasure Hunt

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

Start Hunting!

Translated by