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

 Accepted Answer

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 Comments

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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!